DOC PREVIEW
UCF COP 3502 - Assignment Operator

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

group 1 : 85 and abovegroup 2 : 70 to 84group 3 : 50 to 69group 4 : below 50if (grade >= 85 )group1++ ;else if ( grade >69 )group2++ ;else if ( grade >49 )group3++ ;elsegroup4++;Days in a monthint month, days;switch (month){case 4 :case 6 :case 9 :case 11 :days = 30;break ;case 2 :days = 28;break ;default :days = 31;while and do…whileUse a while loop to print out the square of the numbers entered by the user. It should stop when the user enters – 999.PointersLet us consider the declarations:int x, y;int *p1 ,*p2;This will cause 4 memory locations to be allocated. Let us also assign some values to x and y:x = - 42;y = 163;The memory map may look like this.Let us assign p1 and p2 to point to addresses of x and y:p1 = &x;p2= &y;*p1 is just another name for x after the above declaration. It can be assigned a different value if desired. Let us assign*p1 = 56;This will change whatever is stored in location p1.Brief Review of ‘C’:Assignment Operatora = 4;b = a * a ;c = b + a;‘C’ compiler assigns a separate memory location to every new variable on the left side of the assignment operator.if and if…else if (condition expression) statement1; statement2; Statement 2 will be executed anyway.if (condition expression) statement1; else statement2; statement 3;Grades for 220 students of section 1 of COP3223 are available foran exam. Students are to be grouped as per following scheme:• group 1 : 85 and above• group 2 : 70 to 84• group 3 : 50 to 69• group 4 : below 50It is desired to find out the number of students in each group. #include<stdio.h>int main ( ) {int grade, group1, group2, group3, group4;group1= group2 = group3 = group4 = 0; for (jj = 1; jj <=220; jj++){printf (“\n enter grade:”); scanf (“%d”, &grade); if (grade >= 85 ) group1++ ; else if ( grade >69 ) group2++ ;else if ( grade >49 ) group3++ ; else group4++;}printf (“\n group1= %d, group2= %d “,group1, group2);printf (“\n group3= %d, group4= %d “,group3, group4);return 0;}Operator precedence:When an expression contains number of operators, they are processed in a particular order! – (unary operators) e.g. – 45* / %+ – < <= >= >== !=&&| |= (assignment operator)if( a < b+c && c==d || a > e )switch statementDays in a monthint month, days;switch (month){ case 4 : case 6 : case 9 : case 11 : days = 30; break ; case 2 : days = 28; break ; default : days = 31;}while and do…while Use a while loop to print out the square of the numbers entered by the user. It should stop when the user enters – 999.aa = 1;while (aa != – 999){ printf ("\n new value= "); scanf ("%d",&aa ); printf ("sq of %d is %d", aa , aa*aa); }do{ printf ("\nnew value= "); scanf ("%d",&aa); printf ("square of %d is %d", aa, aa*aa);}while ( aa != – 999) alternate version: while (1){ printf ("\nnew value= "); scanf ("%d",&aa); if( aa == – 999)break; printf ("sq of %d is %d", aa, aa*aa); }Function calls:• # include <stdio.h> • int max ( int, int) ;• int main ( ) {• int a, b, m ;• scanf ( “ %d %d “, &a, &b );• m = max( a, b ) ;• printf ( “max of %d and %d is %d “,a, b, m );• }• int max ( int n1, int n2 )• { • if ( n1 > n2) return n1;• else return n2; }• Formal parameters: Parameters listed in the function definition ( e.g. n1, n2 in example)• Actual Parameters: Values being passed on by the calling function ( e.g. Main passing on values of a,b)• Number of formal and actual parameters must match• The order of parameters must match ( e.g. a to n1 and b to n2)• The indicated type for each parameter must match the type in the function definitionDo the actual parameter values get changed after the function is called?#include <stdio.h>int func1(int x, int y);int main(void) { int mm, k = 10, j =6; mm = func1(k, j ); printf("\nIn the main program”); printf("mm = %d k=%d j=%d\n",mm, k, j); return 0;}int func1(int x, int y) {int ss;ss = 2 * x + y;x = x + 10;y = y * 2; printf("In func1: x=%d y=%d\n", x,y); return ss;}output:In func1: x=20 y=12In the main programmm = 26 k=10 j=6This is known as parameter passing by value. The parameters x and y in the called function, correspond to the parameters k and j of the main function.They get their values from the main function, Their values get changed in the called function, BUT the change DOES NOT AFFECT the original values of k and j inside the main function.Library Functions:C compiler provides a number of library functions. A nice collection of most commonly used functions can be found in http://www.cs.ucf.edu/courses/cop3502/spr04/recitation/clibs.docYou can also create your own libraries and use them in various client programs. The access is provided through interfaces. An interface is the boundary between the implementation of the library and the client programs. The purpose of the interface is to provide each client the relevant information needed to use the library, without showing the detailed implementation. The interface is provided by a header file of the same name that implements the library functions with extension .h. Suppose all the library functions are included in a file libraries.c. Then create another file called libraries.h. which just contain the corresponding functions prototypes. To enable a client program to use the libraries, you have to just include libraries.h in the client program. Also make sure that you have included it in libraries.c.ARRAYS:Array size must be declared in the beginning of the programchar names[200]; int grades[200];Consider grades obtained by 80 students in 3 tests. Test 1 Test 2 Test384 76 7067 60 8278 56 7591 85 9062 55 50… …. ….… …. ….Let us say we want to findi) average performance of each student in the 3 testsii) mean grade for each testLet us first store the grades in a two dimensional arrayint grades[80][3]; double perform[80], double mean[3];The grades can be entered from the keyboard row-wise usingfor ( j = 0; j < 80 ; j++ ) scanf ( “ %d %d %d “,&grades [j][1], &grades [j][2], &grades[j][3] );To find the performance of each student, the outer loop variable refers to each student• for ( j = 0 ; j < 80 ; j++ ) • {• sum= 0;• for ( k = 0 ; k < 3 ; k ++ ) • sum = sum + grades [ j ] [ k ] ;• perform[j] = sum / 3;• printf


View Full Document

UCF COP 3502 - Assignment Operator

Download Assignment Operator
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Assignment Operator and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Assignment Operator 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?