Você está na página 1de 12

Data Structure Homework Title / No.

: ___homework 1___Course Code : _________ Course Instructor : ______ Course Tutor (if applicable) : __ Date of Allotment : _______________ Date of submission : _____ Students Roll No.________________ Section No. : ___ Declaration: I declare that this assignment is my individual work. I have not copied from any other students work or from any other source except where due acknowledgment is made explicitly in the text, nor has any part been written for me by another person. Students Signature:

xyz

Q1) An array contains 25 positive integers.Write a module which: A)Finds all the require of elements whose sum is 25. Ans:
Suppose in the array, the elements are: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 So there are the list of elements whose sum is 25: A)20+5=25 B)15+10=25 C)10+10+5=25 D)9+6+10=25 E)12+7+6=25 etc.

So here I am writing a program to calculate sum of any number in the array and sum=25: #include<studio.h> #include<conio.h> main() { int a[25], EVNUM =0, ODNUM =0,i,sum1=0sum2=0, sum3=0, sum4=0, sum5=0; for(i=0;i<25;i++) scanf("%d",&a[i]); sum1=sum1+a[19]+a[4]; printf(elements=%d%d,a[19],a[4]); printf(sum=%d,sum1); sum2=sum2+a[14]+a[9]; printf(elements=%d%d,a[14],a[9]); printf(sum=%d,sum); sum3=sum3+a[9]+a[9]+a[4]; printf(elements=%d%d%d,a[9],a[9],a[4]); printf(sum=%d,sum3); sum4=sum4+a[8]+a[5]+a[9]; printf(elements=%d%d%d,a[8],a[5]+a[9]); printf(sum=%d,sum4); sum5=sum5+a[11]+a[6]+a[5]; printf(elements=%d%d%d,a[11],a[6]+a[5]}); printf(sum=%d,sum5); getch(); }

B)Find the number EVNUM of element of A which are even and the number ODNUM of element of A which are odd. Ans:
#include<studio.h> #include<conio.h> main() { int a[25], EVNUM =0, ODNUM =0,i; for(i=0;i<25;i++) scanf("%d",&a[i]); /* display the number of odd and even intergers */ for(i=0;i<25;i++) { if((a[i]%2 ==0)) EVNUM ++; if((a[i]%2==1)) ODNUM ++; } printf("%d %d",count_even,count_odd); getch(); }

Q2) Each student of a class of 30 students takes 6 test in which scoresranges from 0 to 100 .Suppose the test Scores are stored in 30*6 array test. Write a module which a) Find the average grade for each test? Ans:

#include<stdio.h> #include<conio.h> int main(){ int st[60][7],b,c,d,avg; int s,t; int total; clrscr(); printf("\nenter marks of students:%d",s); for(b=1;b<=60;b++) { printf("\n enter marks for student :); for(c=1;c<=6;c++) { printf("\nenter marks of test%d,t); scanf(%d,&D[b][c]); }} for(b=1;b<=60;b++) { Printf("\nStudent:%d",s); total=0; for(c=1;c<=6;c++) { Printf("\nTest:%d",t); if(st[s][t]<33){ printf("Grade:F"); } else if(st[b][c]>=33 && st[b][c]<60) { Printf("Grade:C"); }else if(st[b][c]>=60 && st[b][c]<70) { Printf("Grade:B"); } else if(st[b][c]>=70 && st[b][c]<80){ printf("Grade:A"); }else if(st[b][c]>=80 && st[b][c]<=90) {

Printf("Grade:A+"); } else if(st[b][c]>90) { Printf("Distintion"); }} Printf("\n"); getch(); return 0; }

b) Find the final grade for each student when the final grade is the average of the students five highest test scores? ANS:
#include<stdio.h> #include<conio.h> int main(){ int st[30][30],a,m,sc,total,avg; int total;clrscr(); printf("\nenter marks of students:"); for(a=1;a<=60;a++){ printf("\n enter marks for student :%d ",s); for(m=1;m<=6;m++){ printf("\nenter marks of test%d,t); scanf(%d,st[a][m]); }} for(a=1;a<=60;a++) { Printf("\nStudent:%d",s); total=0; for(m=1;m<=6;m++) {total=total+st[a][m];} Printf("\n"); Printf("\nTotal score:%d",total); if(total<200){ printf("Grade:F");

} else if ( total>=200 && total<360){ Printf("Grade:C"); }else if(total>=360 && total<420){ Printf("Grade:B"); }else if(total>=420 && total<480){ Printf("Grade:A");}else if(total>=480 && total<=540){ Printf("Grade:A+");}else if(total>540){ Printf("Distintion"); }}getch();return 0;}
c)

Find the number NUM of student who has failed i.e whose final grade is less than 60?

ANS:
#include<stdio.h> #include<conio.h> int main(){ int st[30][30],a,m,sc,avg,ctr=0;int total; clrscr(); Printf("\nenter marks of students:"); for(a=1;a<=5;a++){ Printf("\n enter marks for student :%d ",s); for(m=1;m<=6;m++){ Printf("\nenter marks of test %d",t); Scanf(%d,&st[a][m]); }}for(a=1;a<=5;a++){ Printf("\nStudent:%d",s); total=0;for(m=1;m<=6;m++){ total=total+st[a][m];}if(total<60){ctr++;} Printf("\nTotal score:%d",total); } Printf("%d,ctr); getch();return 0;}

d) Find the average of the final grades?

ANS:
#include<stdio.h> #include<conio.h> int main() { int st[30][30],a,m,sc,avg,ctr=0;int total;clrscr(); Printf("\nenter marks of students:"); for(a=1;a<=5;a++){ Printf("\n enter marks for student :%d ",s); for(m=1;m<=6;m++) { Printf("\nenter marks of test%d ,t); Scanf(%d,&st[a][m]); }} for(a=1;a<=5;a++){ Printf("\nStudent:%d",s); total=0; for(m=1;m<=6;m++){ total=total+st[a][m];avg=total/6;} Printf("\nTotal score:%d",total); Printf("\nAVG:%d",avg);} getch(); return 0; }

Q3) Given an array A[10,7] is stored in row major order. An element A[3,3] is stored in a row order at address 1064 and 1124. Find the address of A[4,6] and A[1,2]?

Ans: #include <stdio.h> #include <conio.h> void main() { int a[3][3], i, j; clrscr(); printf("\n\t Enter matrix of 3*3 : "); for(i=0; i<3; i++) { for(j=0; j<3; j++) { scanf("%d",&a[i][j]); //read 3*3 array } } printf("\n\t Matrix is : \n"); for(i=0; i<3; i++) { for(j=0; j<3; j++) { printf("\t %d",a[i][j]); //print 3*3 array } printf("\n"); } Printf(Address of A[4,6] is:%p,&a[4][6]); Printf(Address of A[1,2] is:%p,&a[1][2]); } getch();

Q4) Write an algorithm to the max and minimum values of a2D Array?
1.#include<stdio.h> 2.#include<conio.h> 3.Void main()

4.{ 5. 6. 7. 8.
9. 10.

11. 12. Scanf(Enter Row:%d ",&row) 13. Scanf(\nColumn:%d ",&col); 14. 15. if(arr[row][col]<=min) 16. min=arr[row][col]; 17. if(arr[row][col]>=max) 18. max=arr[row][col]; 19. } 20. printf("The maximum number of Row%d ,row); 21. Printf(" is %d",max); 22. printf("The minimum number of Row=%d ",row); 23. Printf(" is %d",min); 24. 25. 26. } 27. exit(0); 28.

int col=0,row=0; int[][] arr=new int[3][5]; int min=arr[row][col]; int max=arr[row][col]; for(row=0;row<arr.length;row++){ for(col=0;col<arr[row].length;col++){

Q5) Write an algorithm to find occurrence of data in multiple location in an array? Ans:
Given a sorted array arr[] and a number x, write a function that counts the occurrences of x in arr[]. Expected time complexity is O(Logn)

Examples: Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 2 Output: 4 // x (or 2) occurs 4 times in arr[] Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, Output: 1 Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, Output: 2 Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, Output: -1 // 4 doesn't occur in arr[] x = 3 x = 1 x = 4

Method 1 (Linear Search) Linearly search for x, count the occurrences of x and return the count. Time Complexity: O(n) Method 2 (Use Binary Search) 1) Use Binary search to get index of the first occurrence of x in arr[]. Let the index of the first occurrence be i. 2) Use Binary search to get index of the last occurrence of x in arr[]. Let the index of the last occurrence be j. 3) Return (j i + 1); /* if x is present in arr[] then returns the count of occurrences of x, otherwise returns -1. */ int count(int arr[], int x, int n) { int i; // index of first occurrence of x in arr[0..n1] int j; // index of last occurrence of x in arr[0..n-1] /* get the index of first occurrence of x */ i = first(arr, 0, n-1, x, n); /* If x doesn't exist in arr[] then return -1 */ if(i == -1) return i;

/* Else get the index of last occurrence of x. Note that we are only looking in the subarray after first occurrence */ j = last(arr, i, n-1, x, n); /* return count */ return j-i+1;

/* if x is present in arr[] then returns the index of FIRST occurrence of x in arr[0..n-1], otherwise returns -1 */ int first(int arr[], int low, int high, int x, int n) { if(high >= low) { int mid = (low + high)/2; /*low + (high - low)/2;*/ if( ( mid == 0 || x > arr[mid-1]) && arr[mid] == x) return mid; else if(x > arr[mid]) return first(arr, (mid + 1), high, x, n); else return first(arr, low, (mid -1), x, n); } return -1; } /* if x is present in arr[] then returns the index of LAST occurrence of x in arr[0..n-1], otherwise returns -1 */ int last(int arr[], int low, int high, int x, int n) { if(high >= low) { int mid = (low + high)/2; /*low + (high - low)/2;*/ if( ( mid == n-1 || x < arr[mid+1]) && arr[mid] == x ) return mid;

else if(x < arr[mid]) return last(arr, low, (mid -1), x, n); else return last(arr, (mid + 1), high, x, n); } return -1; } /* driver program to test above functions */ int main() { int arr[] = {1, 2, 2, 3, 3, 3, 3}; int x = 3; // Element to be counted in arr[] int n = sizeof(arr)/sizeof(arr[0]); int c = count(arr, x, n); printf(" %d occurs %d times ", x, c); getchar(); return 0; }

Você também pode gostar