c programming language

15
C Programming Language Course Number: 11103 Instructor: ZAID ARKHAGHA , [email protected] Text book: C- How to Program, by Deitel and Deitel, Printice Hall CHAPTERS : 1,2,3,4,5,6,7

Upload: zaid-arkhagha

Post on 20-Jan-2015

1.417 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: C  programming language

C Programming Language

Course Number: 11103

Instructor: ZAID ARKHAGHA , [email protected]

Text book: C- How to Program, by Deitel and Deitel, Printice Hall

CHAPTERS : 1,2,3,4,5,6,7

Page 2: C  programming language

Chapter 1- 4 :

Fundamentals of C : 1. Characters :

a. Number ( 0 – 9 ) b. Letters ( A – Z / a – z ) c. Special Characters + / * - % & $ # @ ! . …

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 :

Type-

name Type Range

Int integer number 4 byte

float floating point

number 8 byte

double double float number 16 byte

char Character 1 byte

%d Int %i Int

%c char %lf double %f float %S string

Page 3: C  programming language
Page 4: C  programming language

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: %d\n, physics: %d\n, Computer skill:%d\n", G1,G2,G3); printf("Average: %.3f\n", Av); return o; }

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; }

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:

#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; }

Page 5: C  programming language

Chapter 5 : Functions

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 %d\n",z); printf("The second result is %d\n", 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

Recursion functions :

Recursive requires: 1- Base 2- Recursive formula EX:

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 ; }

Page 6: C  programming language

EX: int main( ) { int x =7 ; { Int x =5 ; Printf(“X = %d \n”,x); } Printf(“X = %d \n”,x); }

X = 5 X = 7

=========================================== 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 : %d\n",i, value); } return 0; }

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 :

int mul ( ) {

srand ( time(NULL) ); int a , b , s ; a = 2+ ( rand() % 49); b = 2+ ( rand() % 49) ; s = a * b ; return s ; }

Page 7: C  programming language

Chapter 6 : Arrays

-------------

------------------

Page 8: C  programming language
Page 9: C  programming language
Page 10: C  programming language

Strings:

#include <stdio.h>

int main ()

{

char greeting[6] = {'H', 'e', 'l', 'l', 'o',

'\0'};

printf("Greeting message: %s\n", greeting );

return 0;

}

Greeting message: Hello

Page 11: C  programming language

Chapter 7 : pointers

Page 12: C  programming language

: اسئلة اضافية

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; }

Page 13: C  programming language

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%7d\n",n,f(n)); return 0; }

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 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 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("hello\n");

Page 14: C  programming language

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.2f\n",sum); printf("product is %5.2f\n",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"); } } }

Page 15: C  programming language

Write a loop that finds the smallest element in an integer array called data containing 100 elements.

Void main () } double min=data[0]; for (int i=1;i<100;i++) if (data[i]<min) min=data[i]; printf("min is %5.2lf\n",min); } 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; }

What is the output of the following program, assuming that the address of x is 003674D0, the address of a is 00367038?

#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); }