1 / 68

REACH CECS 130 Final Test Review Spring 2013

REACH CECS 130 Final Test Review Spring 2013. How to create arrays. data_type array_name [number-of-elements]; Two Dimensional Array array_type array_name [ number_of_ROWS ][ number_of_COLUMNS ];. How to create pointers. type* pointer_name; ex. int my_int;

fawzia
Download Presentation

REACH CECS 130 Final Test Review Spring 2013

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. REACH CECS 130 Final Test Review Spring 2013

  2. How to create arrays • data_typearray_name [number-of-elements]; • Two Dimensional Array array_typearray_name [number_of_ROWS][number_of_COLUMNS];

  3. How to create pointers • type* pointer_name; • ex. int my_int; int* my_int_pointer = &my_int; Assigns the address of my_int to the pointer

  4. Useful string functions • Copying strings from one to another • char* strcpy(char* p, const char* q); • char s[6]; strcpy(s, “Hello”); • To combine strings • char* strcat(char* p, const char* q); • char s[12] = “Hello” strcat(s, “World”);

  5. Useful string functions • To copy n characters from q to the of p. • char* strncpy(char* p, const char* q, int n); • char s [7] = “Say “; char t[] = “Hi”; strncpy (s, t, 2)

  6. String functions

  7. Challenge question • Can you write a program using C++ that uses a FOR loop to initialize a 2D array that looks like the following {0,5,10,15}{0,2,4,6}

  8. Answer #include<iostream> using namespace std; int main(){ int array[2][4], , row, column; for(row=0;row<2;row++) for(column=0;column<4;column++){ if(row==0) array[row][column]=column*5; else if(row==1) array[row][column]=column*2; } for(row=0; row<2; row++){ for(column =0; column <4; column ++) cout<<array[row][column]<<" "; cout<<endl; } system("pause"); return 0; }

  9. Classes • Classes are general models from which you can create objects • Classes have data members either data types or methods • Classes should contain a constructor method and a destructor method • See handout for example of a program that utilizes a class

  10. Declaring Classes class ClassName { memberList }; memberList can be either data member declarations or method declarations

  11. Class Declaration Example Class Bow { //data member declarations string color; bool drawn; intnumOfArrows; Bow(string aColor); //constructor ~Bow(); //destructor //methods void draw(); int fire(); };

  12. Creating Methods Return_type ClassName::methodName(argumentList) { methodImplementation }

  13. Methods Creation Example //draws the bow void Bow::draw() { drawn = true; cout<< “The “<<color<<“bow has been drawn.”<<endl; }

  14. Simple Data Structures

  15. Passing Structures to Functions

  16. Sizeof()

  17. Malloc() Please enter how long your name is: 21 Please enter your name: Nawaf Hello Nawaf Please enter how long your name is: -7 Failed allocation memory

  18. free()

  19. Calloc and Realloc() int *n; int * n1; n=( int * ) calloc(5, sizeof(int)); // Reserves a block of memory for 5 integers //Decide you need to reallocate more memory later in the program n1= (int *) realloc(n, 10 * sizeof(int));//allocate 10 integers instead of 5 if (n1!=NULL) { n=n1; } else printf("Out of memory!"); realloc() returns null if unable to complete or a pointer to the newly reallocated memory.

  20. How to declare and implement functions • Function declaration • Function definition • Function call

  21. #include <iostream> using namespace std; int add(int, int); int main(void) { int number1, number2; cout << “Enter the first value to be summed:”; cin >> number1; cout << “\nEnter the second:”; cin >> number2; cout << “\n The sum is: “ << add (number1, number2) <<endl; } int add(int a, int b){return a+b;}

  22. Functions Challenge • Write a function, called multiply that multiplies two numbers and returns the result

  23. C - Files Do you know the syntax for each of these, used to read and write to data files? • Pointers: think of it as the memory address of the file • fopen() • fclose() • fscanf() • fprintf()

  24. fopen(“file name”, “Mode”) • fopen() returns a FILE pointer back to the pRead variable • #include <cstdio> • Main() • { FILE *pRead; • pRead = fopen(“c:\\folder1\\folder2\\file1.dat”, “r”); • if(pRead == NULL) • printf(“\nFile cannot be opened\n”); • else • printf(“\nFile opened for reading\n”); • fclose(pRead); • }

  25. What does this code do? int main () { FILE * pFile; char c; pFile=fopen("alphabet.txt","wt"); for (c = 'A' ; c <= 'Z' ; c++) { putc (c , pFile);//works like fprintf } fclose (pFile); return 0; }

  26. Common Text File Modes

  27. fclose(file pointer) • Pretty basic. • Always close files when you use fopen.

  28. fscanf(FILE pointer, “data type”, variable in which to store the value) • Reads a single field from a data file • “%s” will read a series of characters until a white space is found • can do fscanf(pRead, “%s%s”, name, hobby);

  29. #include <stdio.h> • Main() • { • FILE *pRead; • char name[10]; • pRead = fopen(“names.dat”, “r”); • if( pRead == NULL ) • printf( “\nFile cannot be opened\n”); • else • printf(“\nContents of names.dat\n”); • fscanf( pRead, “%s”, name ); • while( !feof(pRead) ) { // While end of file not reached • printf( “%s\n”, name ); // output content of name • fscanf( pRead, “%s”, name ); // scan from file next string • } • fclose(pRead); • }

  30. Quiz Kelly 11/12/86 6 Louisville Allen 04/05/77 49 Atlanta Chelsea 03/30/90 12 Charleston Can you write a program that prints out the contents of this information.dat file?

  31. #include <stdio.h> • Main() • { • FILE *pRead; • char name[10]; • char birthdate[9]; • float number; • char hometown[20]; • pRead = fopen(“information.dat”, “r”); • if( pRead == NULL ) • printf( “\nFile cannot be opened\n”); • else • fscanf( pRead, “%s%s%f%s”, name, birthdate, &number, hometown ); • while( !feof(pRead) ) { • printf( “%s \t %s \t %f \t %s\n”, name, birthdate, number, hometown ); • fscanf( pRead, “%s%s%f%s”, name, birthdate, &number, hometown ); • } • fclose(pRead); • }

  32. fprintf(FILE pointer, “list of data types”,list of values or variables) • The fprintf() function sends information (the arguments) according to the specified format to the file indicated by stream. fprintf() works just like printf() as far as the format goes.

  33. #include <stdio.h> Main() { FILE *pWrite; char fName[20]; char lName [20]; float gpa; pWrite = fopen(“students.dat”,”w”); if( pWrite == NULL ) printf(“\nFile not opened\n”); else printf(“\nEnter first name, last name, and GPA ”); printf(“separated by spaces:”); scanf(“%s%s%f”, fName, lName, &gpa); fprintf(pWrite, “%s \t %s \t % .2f \n”, fName, lName, gpa); fclose(pWrite); }

  34. Quiz • Can you write a program that asks the user for their • Name • Phone Number • Bank account balance And then prints this information to a data file called accounts.dat ?

  35. exit() /* exit example */ #include <stdio.h> #include <stdlib.h> int main () { FILE * pFile; pFile = fopen ("myfile.txt","r"); if (pFile==NULL) { printf ("Error opening file"); exit (EXIT_FAILURE); } else { /* file operations here */ } return 0; }

  36. perror() /* perror example */ #include <stdio.h> int main () { FILE * pFile; pFile=fopen ("unexist.ent","rb"); if (pFile==NULL) perror ("The following error occurred"); else fclose (pFile); return 0; }

  37. Function overloading • void swap (int *a, int *b) ; • void swap (float *c, float *d) ; • void swap (char *p, char *q) ; • The other way is to have different number of input parameters for the function

  38. Default Arguments int boxVolume(int length = 1, int width = 1,int height = 1) { return (length * width * height); } If the function was called with no parameters then the default values will be used.

  39. Scope :: • Used to go out one level. • If you have a global and local variables with same name, and need to call global from local scope then you need to use ::VariableName

  40. Static vs. Automatic variables • All your declared variables are automatic. • Static variables keep there values as long as they exist.

  41. OOP • Declare classes • Create objects • 3 MAIN PRINCIPLES OF OOP • Data abstraction – hiding data members and implementation of a class behind an interface so that the user of the class corrupt that data • Encapsulation – each class represents a specific thing or concept. Multiple classes combine to produce the whole • Polymorphism-objects can be used in more than one program

  42. Classes • Classes are general models from which you can create objects • Classes have data members either data types or methods • Classes should contain a constructor method and a destructor method • See handout for example of a program that utilizes a class

  43. Declaring Classes class ClassName { memberList }; memberList can be either data member declarations or method declarations

  44. Class Declaration Example Class Bow { //data member declarations string color; bool drawn; intnumOfArrows; Bow(string aColor); //constructor ~Bow(); //destructor //methods void draw(); int fire(); };

  45. Creating Methods Return_type ClassName::methodName(argumentList) { methodImplementation }

  46. Methods Creation Example //draws the bow Void Bow::draw() { drawn = true; cout<< “The “<<color<<“bow has been drawn.”<<endl; }

  47. Union Output: 9 3.1416 #include <stdio.h> #include<stdlib.h> union NumericType { int iValue; long lValue; double dValue; }; int main() { union NumericType Values; // iValue = 10 Values.iValue=9; printf("%d\n", Values.iValue); Values.dValue = 3.1416; printf("%f\n", Values.dValue); system("pause"); }

  48. Inline function • An inline function is one in which the function code replaces the function call directly. #include <stdio.h> inline void test(void){ puts("Hello!");} int main () { test(); // This will be replaced with puts("Hello!") on run time return 0; }

More Related