Write a complete CC++ program that defines an integer array of size 50, and prompts the user to fill the array then print the array. After printing the array your program should also prompt the user enter startValue, and endValue. The program should print the number of values that are in between the value startValue and endValue inclusive where startValue < endValue.
Example:
Enter the number of elements (up to 50): 5
Enter 5 integers:
10 5 20 8 15
Array elements: 10 5 20 8 15
Enter startValue and endValue: 5 12
Count of values between 5 and 12: 3
Solution
#include<stdio.h>intmain(){intarr[50];intn,startValue,endValue,count=0;printf("Enter the number of elements (up to 50): ");scanf("%d",&n);printf("Enter %d integers:\n",n);for(inti=0;i<n;i++){scanf("%d",&arr[i]);}printf("Array elements: ");for(inti=0;i<n;i++){printf("%d ",arr[i]);}printf("\n");printf("Enter startValue and endValue: ");scanf("%d %d",&startValue,&endValue);for(inti=0;i<n;i++){if(arr[i]>=startValue&&arr[i]<=endValue){count++;}}printf("Count of values between %d and %d: %d\n",startValue,endValue,count);return0;}
#include<iostream>usingnamespacestd;intmain(){intarr[50];intn,startValue,endValue,count=0;cout<<"Enter the number of elements (up to 50): ";cin>>n;cout<<"Enter "<<n<<" integers:\n";for(inti=0;i<n;i++){cin>>arr[i];}cout<<"Array elements: ";for(inti=0;i<n;i++){cout<<arr[i]<<" ";}cout<<"\n";cout<<"Enter startValue and endValue: ";cin>>startValue>>endValue;for(inti=0;i<n;i++){if(arr[i]>=startValue&&arr[i]<=endValue){count++;}}cout<<"Count of values between "<<startValue<<" and "<<endValue<<": "<<count<<"\n";return0;}
Exercise 2
Write a complete CC++ program that defines an integer array of size 50, and prompts the user to fill the array with grades then print the array. Then compares the sum of even numbers in the array with the sum of odd numbers in the array.
If the sum of even numbers is larger, the program prints 1.
If the sum of odd numbers is larger, the program prints -1.
If both sums are equal, the program prints 0.
Example:
Enter the number of elements (up to 50): 4
Enter 4 grades:
2 3 4 1
Array elements: 2 3 4 1
1
Solution
#include<stdio.h>intmain(){intarr[50];intn,sumEven=0,sumOdd=0;printf("Enter the number of elements (up to 50): ");scanf("%d",&n);printf("Enter %d grades:\n",n);for(inti=0;i<n;i++){scanf("%d",&arr[i]);if(arr[i]%2==0){sumEven+=arr[i];}else{sumOdd+=arr[i];}}printf("Array elements: ");for(inti=0;i<n;i++){printf("%d ",arr[i]);}printf("\n");if(sumEven>sumOdd){printf("1\n");}elseif(sumOdd>sumEven){printf("-1\n");}else{printf("0\n");}return0;}
#include<iostream>usingnamespacestd;intmain(){intarr[50];intn,sumEven=0,sumOdd=0;cout<<"Enter the number of elements (up to 50): ";cin>>n;cout<<"Enter "<<n<<" grades:\n";for(inti=0;i<n;i++){cin>>arr[i];if(arr[i]%2==0){sumEven+=arr[i];}else{sumOdd+=arr[i];}}cout<<"Array elements: ";for(inti=0;i<n;i++){cout<<arr[i]<<" ";}cout<<"\n";if(sumEven>sumOdd){cout<<"1\n";}elseif(sumOdd>sumEven){cout<<"-1\n";}else{cout<<"0\n";}return0;}
Exercise 3
Write a complete CC++ program that defines an integer array of size 50, and prompts the user to fill the array with grades then print the array. Then program then prints the number of the elements in the array that are multiples of five.
Example:
Enter the number of elements (up to 50): 7
Enter 7 grades:
15 17 413 1005 3 70 2
Array elements: 15 17 413 1005 3 70 2
Multiples of five: 15 1005 70
Total count: 3
Solution
#include<stdio.h>intmain(){intarr[50];intn,count=0;printf("Enter the number of elements (up to 50): ");scanf("%d",&n);printf("Enter %d grades:\n",n);for(inti=0;i<n;i++){scanf("%d",&arr[i]);}printf("Array elements: ");for(inti=0;i<n;i++){printf("%d ",arr[i]);}printf("\n");printf("Multiples of five: ");for(inti=0;i<n;i++){if(arr[i]%5==0){printf("%d ",arr[i]);count++;}}printf("\nTotal count: %d\n",count);return0;}
#include<iostream>usingnamespacestd;intmain(){intarr[50];intn,count=0;cout<<"Enter the number of elements (up to 50): ";cin>>n;cout<<"Enter "<<n<<" grades:\n";for(inti=0;i<n;i++){cin>>arr[i];}cout<<"Array elements: ";for(inti=0;i<n;i++){cout<<arr[i]<<" ";}cout<<"\n";cout<<"Multiples of five: ";for(inti=0;i<n;i++){if(arr[i]%5==0){cout<<arr[i]<<" ";count++;}}cout<<"\nTotal count: "<<count<<"\n";return0;}
Exercise 4
Write a complete CC++ program that defines two integer arrays each of size 50, the prompts the user to fill the arrays with integers. Then program prompts the user to enter the value of an integer variable named Append. The program should combine the arrays A & B and store the final results in C as follows:
If Append is equal to 1 the program appends A to B.
If Append is equal to 0 the function appends B to A.
Example:
Enter number of elements for A: 4
Enter elements for A:
10 20 30 2
Enter number of elements for B: 3
Enter elements for B:
2 34 8
Enter Append value (0 or 1): 1
Result Array C: 2 34 8 10 20 30 2
Solution
#include<stdio.h>intmain(){intA[50],B[50],C[100];intnA,nB,nC=0,appendChoice;printf("Enter number of elements for A: ");scanf("%d",&nA);printf("Enter elements for A:\n");for(inti=0;i<nA;i++)scanf("%d",&A[i]);printf("Enter number of elements for B: ");scanf("%d",&nB);printf("Enter elements for B:\n");for(inti=0;i<nB;i++)scanf("%d",&B[i]);printf("Enter Append value (0 or 1): ");scanf("%d",&appendChoice);if(appendChoice==1){for(inti=0;i<nB;i++){C[nC]=B[i];nC++;}for(inti=0;i<nA;i++){C[nC]=A[i];nC++;}}elseif(appendChoice==0){for(inti=0;i<nA;i++){C[nC]=A[i];nC++;}for(inti=0;i<nB;i++){C[nC]=B[i];nC++;}}printf("Result Array C: ");for(inti=0;i<nC;i++){printf("%d ",C[i]);}printf("\n");return0;}
#include<iostream>usingnamespacestd;intmain(){intA[50],B[50],C[100];intnA,nB,nC=0,appendChoice;cout<<"Enter number of elements for A: ";cin>>nA;cout<<"Enter elements for A:\n";for(inti=0;i<nA;i++)cin>>A[i];cout<<"Enter number of elements for B: ";cin>>nB;cout<<"Enter elements for B:\n";for(inti=0;i<nB;i++)cin>>B[i];cout<<"Enter Append value (0 or 1): ";cin>>appendChoice;if(appendChoice==1){for(inti=0;i<nB;i++)C[nC++]=B[i];for(inti=0;i<nA;i++)C[nC++]=A[i];}elseif(appendChoice==0){for(inti=0;i<nA;i++)C[nC++]=A[i];for(inti=0;i<nB;i++)C[nC++]=B[i];}cout<<"Result Array C: ";for(inti=0;i<nC;i++){cout<<C[i]<<" ";}cout<<"\n";return0;}
Exercise 5
Write a complete CC++ program that defines an integer array of size 50, and prompts the user to fill the array with quiz grades (between 0 and 10 inclusive). Then the program computes the frequency of each value in the array and prints the most frequent element.
Example:
Enter the number of elements (up to 50): 10
Enter 10 quiz grades (0-10):
5 8 5 9 5 8 10 5 7 5
Most frequent element is: 5 (appeared 5 times)
Solution
#include<stdio.h>intmain(){intarr[50];intfreq[11]={0};intn;printf("Enter the number of elements (up to 50): ");scanf("%d",&n);printf("Enter %d quiz grades (0-10):\n",n);for(inti=0;i<n;i++){scanf("%d",&arr[i]);if(arr[i]>=0&&arr[i]<=10){freq[arr[i]]++;}}intmaxFreq=-1;intmostFrequentVal=-1;for(inti=0;i<=10;i++){if(freq[i]>maxFreq){maxFreq=freq[i];mostFrequentVal=i;}}printf("Most frequent element is: %d (appeared %d times)\n",mostFrequentVal,maxFreq);return0;}
#include<iostream>usingnamespacestd;intmain(){intarr[50];intfreq[11]={0};intn;cout<<"Enter the number of elements (up to 50): ";cin>>n;cout<<"Enter "<<n<<" quiz grades (0-10):\n";for(inti=0;i<n;i++){cin>>arr[i];if(arr[i]>=0&&arr[i]<=10){freq[arr[i]]++;}}intmaxFreq=-1;intmostFrequentVal=-1;for(inti=0;i<=10;i++){if(freq[i]>maxFreq){maxFreq=freq[i];mostFrequentVal=i;}}cout<<"Most frequent element is: "<<mostFrequentVal<<" (appeared "<<maxFreq<<" times)\n";return0;}
Exercise 6
Write a complete CC++ program including a main and three functions named Max, CountAbove, and IndexOf.
Max() takes an array of integer (grades) and its size as integer as input parameters and returns the maximum grade in the array as integer type.
SecondMax() takes an array of integer (grades) and its size as integer as input parameters and returns the second maximum grade in the array as integer type.
CountAbove() takes an array of integers, its size as integer, and a score value as integer input parameters and returns the number of grades in the array that are more or equal to the input score.
IndexOf() takes an array of integers, its size as integer, and a score value as integer input parameters and returns the location of the first occurrence of the input score. Location means index. If the value is not found, it should return -1.
MostAppeared() takes an array of integers, its size as integer input parameters and returns the value that is most appeared (occurred) within array elements.
FillNoDuplicate() takes an array of integers, its size, and low and high as integer input parameters. This function should fill the array with random numbers from low to high with no duplications.
PrintGrade() takes an array of integers, its size as integer input parameters. This function prints grade values.
Main Function Logic: The main() defines an integer array of size 70 and read the grades values from the user. The main function then:
Calls Max to find the maximum value.
Calls SecondMax to find the second maximum.
Calls CountAbove to compute the number of Passing grades (grades >= 50).
Calls IndexOf to find out the location of a grade entered by the user.
Calls PrintGrade to display the grades values.
Calls MostAppeared to see which element appeared most.
Finally, define another array of size 70 and try to fill its values by using FillNoDuplicate, then call PrintGrade in order to see grades for this array.
Example Run:
Enter number of students (up to 70): 5
Enter grades:
50 85 40 90 85
Max Grade: 90
Second Max Grade: 85
Number of Passing Grades (>=50): 4
Enter a grade to find its index: 85
Index of 85: 1
Most Appeared Grade: 85
Grades entered: 50 85 40 90 85
Filling new array with unique random numbers (0-100)...
12 5 23 8 19
Solution
#include<stdio.h>
#include<stdlib.h>
#include<time.h>intMax(intarr[],intsize){intmaxVal=arr[0];for(inti=1;i<size;i++){if(arr[i]>maxVal)maxVal=arr[i];}returnmaxVal;}intSecondMax(intarr[],intsize){intmaxVal=Max(arr,size);intsecMax=-2147483648;for(inti=0;i<size;i++){if(arr[i]!=maxVal&&arr[i]>secMax){secMax=arr[i];}}returnsecMax;}intCountAbove(intarr[],intsize,intscore){intcount=0;for(inti=0;i<size;i++){if(arr[i]>=score)count++;}returncount;}intIndexOf(intarr[],intsize,intscore){for(inti=0;i<size;i++){if(arr[i]==score)returni;}return-1;}intMostAppeared(intarr[],intsize){intmaxCount=0;intmostElement=arr[0];for(inti=0;i<size;i++){intcount=0;for(intj=0;j<size;j++){if(arr[j]==arr[i])count++;}if(count>maxCount){maxCount=count;mostElement=arr[i];}}returnmostElement;}voidFillNoDuplicate(intarr[],intsize,intlow,inthigh){intcount=0;while(count<size){intnum=(rand()%(high-low+1))+low;intfound=0;for(inti=0;i<count;i++){if(arr[i]==num){found=1;break;}}if(!found){arr[count]=num;count++;}}}voidPrintGrade(intarr[],intsize){for(inti=0;i<size;i++){printf("%d ",arr[i]);}printf("\n");}intmain(){srand(time(0));intgrades[70];intn,searchGrade;printf("Enter number of students (up to 70): ");scanf("%d",&n);printf("Enter grades:\n");for(inti=0;i<n;i++){scanf("%d",&grades[i]);}printf("Max Grade: %d\n",Max(grades,n));printf("Second Max Grade: %d\n",SecondMax(grades,n));printf("Number of Passing Grades (>=50): %d\n",CountAbove(grades,n,50));printf("Enter a grade to find its index: ");scanf("%d",&searchGrade);printf("Index of %d: %d\n",searchGrade,IndexOf(grades,n,searchGrade));printf("Most Appeared Grade: %d\n",MostAppeared(grades,n));printf("Grades entered: ");PrintGrade(grades,n);intrandomGrades[70];printf("\nFilling new array with unique random numbers (0-100)...\n");FillNoDuplicate(randomGrades,n,0,100);PrintGrade(randomGrades,n);return0;}
#include<iostream>
#include<cstdlib>
#include<ctime>usingnamespacestd;intMax(intarr[],intsize){intmaxVal=arr[0];for(inti=1;i<size;i++){if(arr[i]>maxVal)maxVal=arr[i];}returnmaxVal;}intSecondMax(intarr[],intsize){intmaxVal=Max(arr,size);intsecMax=-2147483648;for(inti=0;i<size;i++){if(arr[i]!=maxVal&&arr[i]>secMax){secMax=arr[i];}}returnsecMax;}intCountAbove(intarr[],intsize,intscore){intcount=0;for(inti=0;i<size;i++){if(arr[i]>=score)count++;}returncount;}intIndexOf(intarr[],intsize,intscore){for(inti=0;i<size;i++){if(arr[i]==score)returni;}return-1;}intMostAppeared(intarr[],intsize){intmaxCount=0;intmostElement=arr[0];for(inti=0;i<size;i++){intcount=0;for(intj=0;j<size;j++){if(arr[j]==arr[i])count++;}if(count>maxCount){maxCount=count;mostElement=arr[i];}}returnmostElement;}voidFillNoDuplicate(intarr[],intsize,intlow,inthigh){intcount=0;while(count<size){intnum=(rand()%(high-low+1))+low;intfound=0;for(inti=0;i<count;i++){if(arr[i]==num){found=1;break;}}if(!found)arr[count++]=num;}}voidPrintGrade(intarr[],intsize){for(inti=0;i<size;i++){cout<<arr[i]<<" ";}cout<<"\n";}intmain(){srand(time(0));intgrades[70];intn,searchGrade;cout<<"Enter number of students (up to 70): ";cin>>n;cout<<"Enter grades:\n";for(inti=0;i<n;i++){cin>>grades[i];}cout<<"Max Grade: "<<Max(grades,n)<<"\n";cout<<"Second Max Grade: "<<SecondMax(grades,n)<<"\n";cout<<"Number of Passing Grades (>=50): "<<CountAbove(grades,n,50)<<"\n";cout<<"Enter a grade to find its index: ";cin>>searchGrade;cout<<"Index of "<<searchGrade<<": "<<IndexOf(grades,n,searchGrade)<<"\n";cout<<"Most Appeared Grade: "<<MostAppeared(grades,n)<<"\n";cout<<"Grades entered: ";PrintGrade(grades,n);intrandomGrades[70];cout<<"\nFilling new array with unique random numbers (0-100)...\n";FillNoDuplicate(randomGrades,n,0,100);PrintGrade(randomGrades,n);return0;}
Exercise 7
Write a function RemoveDuplicates() that receives an array of integers and its size as input parameters. This function should modify the array in-place to remove any duplicate elements, shifting all subsequent unique elements to the left. The function should return the new logical size of the array.
In main(), prompt the user to input the number of elements and fill the array. Call the function to remove duplicates, then print the unique array elements along with the new size.
Example:
Enter the number of elements: 7
Enter 7 elements: 10 20 10 30 20 10 40
Array after removing duplicates: 10 20 30 40
New size: 4
Solution
#include<stdio.h>intRemoveDuplicates(intarr[],intsize);intmain(){intsize;intarr[50];printf("Enter the number of elements: ");scanf("%d",&size);printf("Enter %d elements: ",size);for(inti=0;i<size;i++){scanf("%d",&arr[i]);}intnewSize=RemoveDuplicates(arr,size);printf("Array after removing duplicates: ");for(inti=0;i<newSize;i++){printf("%d ",arr[i]);}printf("\nNew size: %d\n",newSize);return0;}intRemoveDuplicates(intarr[],intsize){for(inti=0;i<size;i++){for(intj=i+1;j<size;j++){if(arr[i]==arr[j]){for(intk=j;k<size-1;k++){arr[k]=arr[k+1];}size--;j--;}}}returnsize;}
#include<iostream>usingnamespacestd;intRemoveDuplicates(intarr[],intsize);intmain(){intsize;intarr[50];cout<<"Enter the number of elements: ";cin>>size;cout<<"Enter "<<size<<" elements: ";for(inti=0;i<size;i++){cin>>arr[i];}intnewSize=RemoveDuplicates(arr,size);cout<<"Array after removing duplicates: ";for(inti=0;i<newSize;i++){cout<<arr[i]<<" ";}cout<<"\nNew size: "<<newSize<<endl;return0;}intRemoveDuplicates(intarr[],intsize){for(inti=0;i<size;i++){for(intj=i+1;j<size;j++){if(arr[i]==arr[j]){for(intk=j;k<size-1;k++){arr[k]=arr[k+1];}size--;j--;}}}returnsize;}
Exercise 8
Write a function MergeSorted() that receives two pre-sorted integer arrays $A$ and $B$, their sizes, and a third array $C$ of size $sizeA + sizeB$. The function should merge the sorted elements of $A$ and $B$ into $C$ such that $C$ remains sorted.
In main(), read two sorted arrays from the user, call the function to merge them, and print the merged array $C$.
Example:
Enter size of A: 3
Enter 3 sorted elements for A: 1 5 8
Enter size of B: 4
Enter 4 sorted elements for B: 2 4 9 10
Merged array C: 1 2 4 5 8 9 10
Solution
#include<stdio.h>voidMergeSorted(intA[],intsizeA,intB[],intsizeB,intC[]);intmain(){intsizeA,sizeB;intA[50],B[50],C[100];printf("Enter size of A: ");scanf("%d",&sizeA);printf("Enter %d sorted elements for A: ",sizeA);for(inti=0;i<sizeA;i++){scanf("%d",&A[i]);}printf("Enter size of B: ");scanf("%d",&sizeB);printf("Enter %d sorted elements for B: ",sizeB);for(inti=0;i<sizeB;i++){scanf("%d",&B[i]);}MergeSorted(A,sizeA,B,sizeB,C);printf("Merged array C: ");for(inti=0;i<sizeA+sizeB;i++){printf("%d ",C[i]);}printf("\n");return0;}voidMergeSorted(intA[],intsizeA,intB[],intsizeB,intC[]){inti=0,j=0,k=0;while(i<sizeA&&j<sizeB){if(A[i]<B[j]){C[k++]=A[i++];}else{C[k++]=B[j++];}}while(i<sizeA){C[k++]=A[i++];}// Copy remaining items of B if anywhile(j<sizeB){C[k++]=B[j++];}}
#include<iostream>usingnamespacestd;voidMergeSorted(intA[],intsizeA,intB[],intsizeB,intC[]);intmain(){intsizeA,sizeB;intA[50],B[50],C[100];cout<<"Enter size of A: ";cin>>sizeA;cout<<"Enter "<<sizeA<<" sorted elements for A: ";for(inti=0;i<sizeA;i++){cin>>A[i];}cout<<"Enter size of B: ";cin>>sizeB;cout<<"Enter "<<sizeB<<" sorted elements for B: ";for(inti=0;i<sizeB;i++){cin>>B[i];}MergeSorted(A,sizeA,B,sizeB,C);cout<<"Merged array C: ";for(inti=0;i<sizeA+sizeB;i++){cout<<C[i]<<" ";}cout<<endl;return0;}voidMergeSorted(intA[],intsizeA,intB[],intsizeB,intC[]){inti=0,j=0,k=0;while(i<sizeA&&j<sizeB){if(A[i]<B[j]){C[k++]=A[i++];}else{C[k++]=B[j++];}}while(i<sizeA){C[k++]=A[i++];}while(j<sizeB){C[k++]=B[j++];}}
Exercise 9
An element in an array is called a leader if it is strictly greater than all elements situated to its right. The rightmost element is always a leader. Write a function FindLeaders() that receives an array of integers and its size, and prints all the leaders in the array.
In main(), read the array size and elements, and call FindLeaders() to print the results.
Example:
Enter number of elements: 6
Enter 6 elements: 16 17 4 3 5 2
Leaders in the array: 17 5 2
Solution
#include<stdio.h>voidFindLeaders(intarr[],intsize);intmain(){intsize;intarr[50];printf("Enter number of elements: ");scanf("%d",&size);printf("Enter %d elements: ",size);for(inti=0;i<size;i++){scanf("%d",&arr[i]);}FindLeaders(arr,size);return0;}voidFindLeaders(intarr[],intsize){printf("Leaders in the array: ");intcurrent_max=arr[size-1];intleaders[50];intleaderCount=0;leaders[leaderCount++]=current_max;for(inti=size-2;i>=0;i--){if(arr[i]>current_max){current_max=arr[i];leaders[leaderCount++]=current_max;}}for(inti=leaderCount-1;i>=0;i--){printf("%d ",leaders[i]);}printf("\n");}
#include<iostream>usingnamespacestd;voidFindLeaders(intarr[],intsize);intmain(){intsize;intarr[50];cout<<"Enter number of elements: ";cin>>size;cout<<"Enter "<<size<<" elements: ";for(inti=0;i<size;i++){cin>>arr[i];}FindLeaders(arr,size);return0;}voidFindLeaders(intarr[],intsize){cout<<"Leaders in the array: ";intcurrent_max=arr[size-1];intleaders[50];intleaderCount=0;leaders[leaderCount++]=current_max;for(inti=size-2;i>=0;i--){if(arr[i]>current_max){current_max=arr[i];leaders[leaderCount++]=current_max;}}for(inti=leaderCount-1;i>=0;i--){cout<<leaders[i]<<" ";}cout<<endl;}