• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
C  programming language
 

C programming language

on

  • 659 views

 

Statistics

Views

Total Views
659
Views on SlideShare
659
Embed Views
0

Actions

Likes
1
Downloads
23
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    C  programming language C programming language Document Transcript

    • C Programming Language Course Number: 11103 Instructor: ZAID ARKHAGHA , zaid_arkaga@yahoo.com Text book: C- How to Program, by Deitel and Deitel, Printice Hall CHAPTERS : 1,2,3,4,5,6,7
    • Chapter 1- 4 : Fundamentals of C : 1. Characters : a. Number b. Letters c. Special Characters (0–9 ) ( A–Z / a–z) +/*-%&$#@! .… 2. Keywords : Words used in C EX: int – scanf – do – for – while – if … 3. Numbers : a. Integers : null numbers of the decimal point. b. Real : Number includes the decimal point . c. Real double-precision : 58e4 584 4. Strings and Characters : a. Strings : " " b. Characters : ' ' 5. Variables : Type Variables_name ; Type Variables_name = value ; Variables_name: 1) Numbers or letters . 2) Does not contain special characters except ( _ ) 3) That starts with a letters or ( _ ) . 4) Use large or small letters . 5) Be a suitable length and meaningful . Type : Typename Type Range Int integer number 4 byte float double char %d %c %f floating point number double float number Character Int char float %i %lf %S 8 byte 16 byte 1 byte Int double string
    •  Write down a program which initialize 3 integers, G1,G2,G3, then calculate average as real (float). Print your name as string using a simple printf statement, then grades G1,G2,G3 in different line with nice heading, and finally, print the average: #include <stdio.h> int main ( ) int G1,G2,G3; float Av; scanf("%d%d%d",G1,G2,G3); printf(" My name is Yahia n"); Av= (G1+G2+G3)/3; printf("math: %dn, physics: %dn, Computer skill:%dn", G1,G2,G3); printf("Average: %.3fn", Av); return o; }  Write a c program to print multiplication table : #include<stdio.h> int main(){ int r,i,j,k; printf("Enter the number range: "); scanf("%d",&r); for(i=1;i<=r;i++){ for(j=1;j<=10;j++) printf("%d*%d=%d ",i,j,i*j); printf("n"); } return 0; }  Write a program to produce the following output:  C program to extract last two digits of a given year and print it : #include<stdio.h> int main(){ int yyyy,yy; printf("Enter a year in for digits: "); scanf("%d",&yyyy); yy = yyyy % 100; printf("Last two digits of year is: %02d",yy); return 0; }  Count the number of digits in c programming language : #include<stdio.h> int main(){ int num,count=0; printf("Enter a number: "); scanf("%d",&num); while(num){ num=num/10; count++; } printf("Total digits is: %d",count); return 0; } #include<stdio.h> int main(){ int n=4,I,j,k=1; for(i=1;i<=n;i++) { for(j=I;j<n;j++) printf(“ “); for(j=1;j<=I;j++) printf(“ %d “,k++); printf(“n”); } }  Write C program to find : 12345678 23456789 345678910 #include<stdio.h> int main() { Int c=8 ; for( int I =1 ; I < 3 ; i++ ) { for( int j= I ;j< c ; j++) { printf(“ %d”,j); } C++; Printf(“n”); return 0; }
    • Chapter 5 : Functions Recursion functions :  Recursive requires: 1- Base 2- Recursive formula EX: EX: #include <stdio.h> int subtraction (int a, int b) { int r; r=a-b; return (r); } int main () { int x=5, y=3, z; z = subtraction (7,2); printf(" The first result is %dn",z); printf("The second result is %dn", subtraction (7,2)); printf("The third result is %d n", subtraction (x,y)); z= 4 + subtraction (x,y); printf("The fourth result is %d n",z); return 0; } The first result is 5 The second result is 5 The third result is 2 The fourth result is 6  Read two number and find HCF using Recursion: int hcf( int A , int B ) } if( B != 0 ) Return hcf( B , A%B ); else return A ; }
    • EX:  int main( ) { int x =7 ; { Int x =5 ; Printf(“X = %d n”,x); } Printf(“X = %d n”,x); } X=5 X=7 calculate sum rand of [1,6] and display results: int sum ( ) { int a , b , s ; a = 1+ ( rand() % 6); b = 1+ ( rand() % 6) ; s=a+b; return s ; }  calculate multiply rand of [2,50] and display results and using srand : =========================================== rand function : In general: to generate numbers between integers [a, b] value = a + rand ()%(b - a + 1) ; EX: Rand [21,31] : value= 21 + rand()%11; srand function: srand ( time(NULL) ); EX: if you want to generate 10000 integers between 21 and 31, then the code will be as follows: #include <stdio.h> int main ( ) { srand ( time(NULL) ); for ( i=1; i<=10000 ; i++) { value= 21 + rand ( )%11; printf(" random integer number %d : %dn",i, value); } return 0; } int mul ( ) { srand ( time(NULL) ); int a , b , s ; a = 2+ ( rand() % 49); b = 2+ ( rand() % 49) ; s=a*b; return s ; }
    • Chapter 6 : Arrays ------------- ------------------
    • Strings: #include <stdio.h> int main () { char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'}; printf("Greeting message: %sn", greeting ); return 0; } Greeting message: Hello
    • Chapter 7 : pointers
    • : ‫اسئلة اضافية‬  Get the following output : 1) 1 12 123 1234 12345 #include <strio.h> void main( ) { for (int i=1; i<=9; i++) { for (int j=1; j<=i; j++) printf("%d",j); printf(("n"); } } 2) * ** *** **** ***** #include <stdio.h> int main() { for ( int i=0; i<5; i++) { for (int j=4-i; j>0; j--) printf(" "); for (int j=0; j<=i; j++) printf("*"); printf("n"); } return 0; } 3) * ** *** **** ***** ***** **** *** ** * #include <stdio.h> int main() { for ( int i=0; i<5; i++) { for (int j=0; j<=i; j++) printf("*"); printf("n"); } for ( int i=0; i<5; i++) { for (int j=4; j>=i; j--) printf("*"); printf("n"); } return 0; }
    •  What the output : #include <stdio.h> int f(int n) { if (n==1) return 1; else return f(n-1)*f(n-1)+1; } int main() { printf( "%7s %7s n"," n "," f(n)"); for (int n=1; n<=6;n++) printf("%7d%7dn",n,f(n)); return 0; }  Write a C function that searches for value key in a a 2D array of size 6 by 5. The function should return the row # and col# of location the value was found at if found and return -1 if not found. The function returns these two valeus using arguments passed by reference. void SearchArray (int a[][5], int ROWS, int key, int & locR, int &locC) { locR=-1; locC=-1; for ( int r=0; r<ROWS; r++) for ( int c=0; c<5;c++) if (a[r][c]==key) { LocR=r; locC=c; } }   Write a function that takes inputs of yards and feet (whole numbers) and calculates and returns an output of the total number of miles (a floating-point value). There are 5280 feet per mile. There are 3 feet in a yard. Use appropriate parameter passing and return mechanisms. Use appropriate datatypes. For example, with inputs of 1760 yards and 1320 ft, the result would be 1.25 miles. Call this function from the main function with different sets of values and print the output. float Miles( int yards, int feet) { return (feet+3*yards)/ 5280.0; } Write a C function that has three inputs which are integers. The function returns true if the first number raised to the power of the second number equals the third number. bool f1( int a, int b, int c) { if (pow( (float) a, (float) b)==c) return true; else return false; }  How many times will the loop bodies execute in the following loops? for (int i=1; i<=5;i+=1) for (int j=1;j<=6;j++) for (int k=2;k<=5;k++) printf("hellon");
    • Write a function called Count. This function is passed a double array along with a parameter that indicates the number of elements in the array. It is also passed a double value. The function computes and returns the number of values in the array that are greater than this double value. Write a complete C function to do this operation. There is no printf or scanf in this function. This is only a function, so there is no main routine here! int Count( int a[], int n, int value) { int v; int count=0; for (int i=0 ; i<n; i++) if (a[i]> value) count++; return count; } Write a code fragment using ONE for-loop to calculate the sum and product of the elements of a floating point array. The array is declared as shown below. Include any other declarations and initialization statements necessary to perform the operation. Display the sum and product results after the for-loop using printf. Void main () { float sum=0,prod=1; for (int i=0;i<MAX;i++) { sum+=values[i]; prod*=values[i]; } printf("sum is %5.2fn",sum); printf("product is %5.2fn",prod); } Given the following declaration, what is stored in the 8th element of the array? int list[10] = {1, 2, 3, 4, 5}; void main () { int data[10]; for (int i=0;i<10;i++) data[i]=10-i; } Write a function to have a user enter some number of integers into an array. The integer values must be between -100 and +100 inclusive (+100 and -100 should be accepted as valid inputs). The integer array and the size of the array are passed into the function through parameters. Do not worry about includes. This is only a function, so there is no main routine. The function should fill the array with valid inputs. For invalid input values, inform the user of the error, but do not count that as a valid input. void ReadValues( int a[], int n) { int v; int count=0; while (count <n) { scanf("%d",&v); if ((v>=-100) && (v <=100)) { a[count]=v; count++; } else { printf(" error n"); } } }
    • Write a loop that finds the smallest element in an integer array called data containing 100 elements. What is the output of the following program, assuming that the address of x is 003674D0, the address of a is 00367038? Void main () } double min=data[0]; for (int i=1;i<100;i++) if (data[i]<min) min=data[i]; printf("min is %5.2lfn",min); } #include <stdio.h> int *p, x,y; int a[5]={ 100,200,300,400,500}; int *p2; void main() { p=NULL; x=10; p=&x; printf("1) %d %d %p %p %p n",x,*p,p,&x,&p); p2=&x; printf("2) %d %d %p %p n",x,*p2,p2,&x); p2=a; printf("3) %d %d %p %p n",a[0],*p2,p2,a); p2=&a[2]; printf("4) %d %p %p n",*p2,p2,a); p2++; printf("5) %d %p n",*p2,p2); p=a; y=*(p+2); printf("6) %d %d n",y,a[2]); printf(" loop using pointer n"); for ( int * p3=a; p3 <= &a[4]; p3++) printf("loop: %d %p n",*p3,p3); x=9; int * const p4=&x; *p4=5; printf("7) %d %d n",x,*p4); } Given an array of 100 doubles named data, write a loop that creates the sum of all the array elements. Void main () } double sum=0; for (int i=0;i<10;i++) sum+=data[i]; printf("%5.2lf",sum); } Write a C function that computes that total sum of of a specific col C in a 2D array of size 6 by 5. int Sum (int a[][5], int ROWS, int col) { int sum=0; for ( int r=0; r<ROWS; r++) sum+=a[r][col]; return sum; }