0% found this document useful (0 votes)
8 views

Computer Science Lab

The document contains examples of C code implementing arrays, structures, sorting and calculating averages. It includes 4 examples of arrays, 4 examples of structures and calculations with them, and code to input and sort data as well as calculate student averages.

Uploaded by

seklawifatimah2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Computer Science Lab

The document contains examples of C code implementing arrays, structures, sorting and calculating averages. It includes 4 examples of arrays, 4 examples of structures and calculations with them, and code to input and sort data as well as calculate student averages.

Uploaded by

seklawifatimah2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Fatimah seklawi

Id number: 6011

Average : 67
Arrays TP1 : Ex:1
#include <stdio.h>
int main ( )
{
int n,k;
do
{
printf ("enter n");
scanf ("%d", &n);
}
while (n<0 || n>5 );
float A[5];
int i ;
float x, px = 0;
printf ("enter the coefficients");
for(i=0; i<n; i++)
{
scanf("%f", &A[i]);
}
printf ("Enter x ");
scanf("%f", &x);
for(i=0; i<n; i++)
{
px += A[i]*k;
k*= x;
}
return 1;
}

Ex:2

#include <stdio.h>
int main()
{
int a[3][4], i,j, sum=0;
for(i=0;i<3; i++)
{
for (j=0; j<4; j++)
{
printf("Enter the values of the array", a);
scanf("%d", &a[i][j]);
}
}
for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
{
sum=sum + a[i][j];
}
printf ("sum of %d row is %d\n" , i, sum);
sum=0;
}
for (j=0; j<4; j++)
{
for(i=0; i<3; i++)
{
sum= sum+a[i][j];
}
printf("sum of %d col is %d\n", j, sum);
sum=0;
}
return 1;
}

Ex:3
#include <stdio.h>
int main ( )
{
char str[100];
int i=0, totalwords, count=0;
totalwords = 1;
printf ("Enter your string: ");
gets (str);
for (i=0; str[i]!= '\0'; i++)
{
if (str[i]==' ' )
{
totalwords++;
}
}
printf ("total number of words is %d" , str, totalwords);
while (str[i]!= '\0')
{
if (str[i] != '\0')
count++;
i++ ;
}
printf("the total number of sentences is %d", count);
return 1;
}
Ex:4
#include <stdio.h>
#include <string.h>
int main ( )
{
char str[4][50],temp [50];
int i, j;
printf("Enter 4 words\n");
gets (str);
for (i=0; i<4; i++)
{
for (j=i+1; j<3 ; j++)
{
if (strcmp(str[i], str[j]>0))
{
strcpy ( temp, str[i] );
strcpy ( str[i] , str[j] );
strcpy ( str [j] , temp);
}
}
}
printf("the lexicographical order: \n");
for ( i=0; i<4; i++)
puts (str[i]);
return 0;
}

Structures TP2
Ex:1
#include <stdio.h>
#include <math.h>
typedef struct Point
{
char Name ;
float x,y ;
} Point ;
int main ( )
{
Point P[10] ;
Point P1, P2 ;
float d ;
int i ;
for(i=0 ; i<10 ; i++)
{
printf ("Name: ");
fflush (stdin);
scanf("%c", &P[i]. Name) ;
puts ("2 coordinates: ") ;
scanf("%f%f", &P[i].x, &P[i].y) ;
}
puts ("Point 1");
i= 10 ;
do
{
printf ("Name :") ;
fflush (stdin) ;
scanf("%c", &P1.Name) ;

i=0;
while(i<10 && P1. Name != P[i].Name)
i++;
}
while (i>9);
{
P1=P[i];
puts ("Point2");
i=10;
do
{
printf("Name:");
fflush(stdin);
scanf ("%c", & P2. Name);
i=0 ;
while(i<5 &&P2.Name != P[i].Name)
i++;
}
while ( i<9 );
P2=P[i] ;
d = sqrt((P1.x- P2.x)*(P1.x- P2.x)+( P1.y-P2.y)*( P1.y-P2.y));
printf ("Distance between %c and %c is %f \n", P1.Name ,
P2.Name , d );
return 1;
}
}

Ex:2
#include <stdio.h>
typedef struct items
{
char name [20] ;
float pp ,sp ;
} items ;
int main ( )
{
int i;
items A[10] ;
for (i=0 ; i<10 ; i++)
{
printf ("items name: \n") ;
fflush (stdin );
gets ( A[i].name);
printf ("purchase price :\n");
fflush (stdin) ;
scanf ("%f", &A[i].pp);
printf ("sale price:\n" );
fflush (stdin) ;
scanf ("%f" , &A[i].sp );
}
float gain=0 ;
for (i=0 ; i<10 ; i++)
gain += ( (A [i].pp)-(A[i].sp));
printf ("money earned %f \n", gain);
return 1;
}
Ex:3
# include <stdio.h>
int main ( )
{
int i , k ,j, A[5] ;
for (i=0 ; i<5 ; i++)
{
printf("Enter time one for player %d",i);
scanf("%d",& A [ i ]) ;
printf("Enter time two for the player %d", i) ;
scanf("%d",&k) ;
if (k<A[i])
{
A[i]=k ;
}
for (i=0 ; i<5 ; i++)
{
for(j=i+1; j<5 ; j++)
{
if(A[j]<A[i])
{
k=A[i] ;
A[i]=A[j] ;
A[j]=k;
}
}
}
for (i=0 ; i<5 ; i++)
{
printf("%d", A[i]) ;
}
return 1 ;
}
}
Ex:4
#include <stdio.h>
typedef struct student
{
int studentnum;
float MGrade, phGrade;
float Average;
} student;
int main ( )
{
student S[5];
int i;
for(i=0; i<5; i++)
{
printf("student number is %d \n", i+1);
printf("Input the Math Grade of each student \n");
scanf("%f" , &S[i]. MGrade);
printf ("Input the physics Grades\n");
scanf("%f" , &S[i].phGrade);
S[i].Average= (S[i].MGrade + S[i].phGrade)/2;
}
int max = 0;
for (i=0;i<5;i++)
{
if ( S[i].Average > S[max].Average)
max= i;
}
printf("the student %d has the heighest average", max +1);
printf ("His Average is %f ", S[max].Average );
return 1;
}

You might also like