Array Programming Exercise P-5

Array Exercise

Programming Exercise

Array Programming Exercise P-4

Programming Exercise Looping Structure P-12

Q.11 Write a program that inputs marks of ten students in an array (maximum marks are 100) and counts the number of students that have marks between 80 and 100.

#include<iostream.h>
#include<conio.h>
main()
{
float marks[10], n;
clrscr();
for(n = 0; n<=9; n++)
{
cout<<"Enter marks of student no. "<<n+1<<" ? ";
cin>>marks[n];
}

cout<<"\n\n Students that have marks between 80 and 100 "<<endl;
for(n = 0; n<=9; n++)
if(marks[n] >=80 && marks[n]<=100)
cout<<"Student no. "<<n+1<<" has marks :"<<marks[n]<<endl;
getch();
}

Q.12 Write a program that uses three one-dimensional arrays and inputs 20 values in 1st array and differentiates even and odd values. It stores even values in 2nd array and odd values into 3rd array. The program should display the values of 2nd and 3rd array into two columns with heading EVEN and ODD.

#include<iostream.h>
#include<conio.h>
main()
{
int a[20],b[20], c[20], odd = 0, even = 0, n;
clrscr();
for(n = 0; n<=19; n++)
{
cout<<"Enter value in element "<<n<<" of array a ? ";
cin>>a[n];
if(a[n]%2 == 0)
b[even++] = a[n];
else
c[odd++] = a[n];
}

cout<<"\n\nValues of 2nd and 3rd arrays "<<endl;
cout<<"EVEN \t ODD"<<endl;
for(n = 0; n<even || n<odd; n++)
{
if(n<even)
cout<<b[n]<<"\t";
else
cout<<" \t";
if(n<odd)
cout<<c[n]<<" \t";
else
cout<<" \t";
cout<<endl;
}
getch();
}

Q.13 Write a program that inputs values into an array. It multiples the even values of elements with 5, odd values with 4 and adds the new values of array. Suppose a user inputs the following values in an array:
// 2, 3, 5, 6, 7
// After multiplying the even values of elements with 5, odd values with 4, the new values of the array will be:
// 10, 12, 20, 30, 28
// Sum of array: 100

#include<iostream.h>
#include<conio.h>
main()
{
int a[10], n, s = 0;
clrscr();
for(n = 0; n<=4; n++)
{
cout<<"Enter value in element "<<n<<" of array a ? ";
cin>>a[n];
}

// multiples the even values of elements with 5, odd values with 4 and adds the new values of array
for(n = 0; n<=4; n++)
{
if(a[n]%2 == 0)
a[n] = a[n]*5;
else
a[n] = a[n]*4;
s = s + a[n];
cout<<a[n]<<endl;
}
cout<<"Sum of array : "<<s<<endl;
getch();
}

Q.14 Write a program that inputs ages of different persons in an array and counts the number of persons that have ages between 65 and 80.

#include<iostream.h>
#include<conio.h>
main()
{
int age[10], n, count = 0;
clrscr();
for(n = 0; n<=9; n++)
{
cout<<"Enter age of person no. "<<n+1<<" ? ";
cin>>age[n];
}

for(n = 0; n<=9; n++)
if(age[n] >=65 && age[n]<=80)
count++;
cout<<"\nThe number of persons that have ages between 65 and 80 = "<<count;
getch();
}

Q.15 Write a program that inputs data into a string and searches the ‘vowel’ characters from the string and displays on the screen.

#include<iostream.h>
#include<conio.h>
main()
{
char str[20], n;
clrscr();
cout<<"Enter a string less than 20 letters ? ";
cin>>str;
cout<<"\n Vowel characters from the string are:"<<endl;
for(n = 0; str[n]!='\0'; n++)
{
if(str[n] == 'a' || str[n] == 'A' || str[n] == 'e' || str[n] == 'E' || str[n] == 'i' || str[n] == 'I'
|| str[n] == 'o' || str[n] == 'O' || str[n] == 'u' || str[n] == 'U')
cout<<str[n]<<endl;
}
getch();
}

Q.16 Write a program that inputs fifteen integer values in an array. It finds out all the prime numbers stored in an array and displays the result on screen.

#include<iostream.h>
#include<conio.h>
main()
{
int arr[15], n, count = 0, d, p;
clrscr();
for(n = 0;n<=14; n++)
{
cout<<"Enter a value in element "<<n<< " of the array ? ";
cin>>arr[n];
}

for(n = 0;n<=14; n++)
{
p = 1;
for(d = 2; d < arr[n]; d++)
if (arr[n]%d ==0)
{
p = 0;
break;
}
if (p == 1)
{
cout<<arr[n]<<endl;
count++;
}
}
cout<<"\n Prime numbers stored in an array are:"<<count;
getch();
}

Q.17 Write a program that inputs fifteen integer values in an array. It finds out the number of occurrences of each value in the array and displays result on display screen. Suppose value 16 is stored 2 times in the array then it displays message “The occurrence of 16 is 2 times”.

#include<iostream.h>
#include<conio.h>
main()
{
int arr[15], i, j, n, count = 0;
clrscr();
for(i = 0;i<=14; i++)
{
cout<<"Enter a value in element "<<i<< " of the array ? ";
cin>>arr[i];
}

for(i = 0; i<=14; i++)
{
if(arr[i] == -1)
continue;
n = arr[i];
count = 1;
for(j = i+1;j<=14; j++)
{
if(arr[j] == n)
{
count++;
arr[j] = -1;
}
}
cout<<"The occurrence of "<<n<< " is " <<count<< " times "<<endl;
}
getch();
}

Q.18 Write a program that searches a value in an array using Sequential Search method and replaces it with another one.

#include<iostream.h>
#include<conio.h>
main()
{
int arr[5], n, i, pos;
clrscr();
i = 0;
while(i<=4)
{
cout<<"Enter value in element "<<i<<"? "; 
cin>>arr[i];
i++;
}
pos = 0;
cout<<endl<<"Enter a value to search ? ";
cin>>n;
i = 0;
while(i<=4)
{
if(n == arr[i])
{
pos = i;
break;
}
i++;
}
if(pos == 0)
cout<<"Value not found "<<endl;
else
{
cout<<"Value found at position = "<<pos<<endl;

cout<<endl<<"Enter a value to replace ? ";
cin>>arr[pos];
cout<<endl<<"Data of array after replacing value :"<<endl;
for(i= 0;i<=4;i++)
cout<<arr[i]<<endl;
}
getch();
}

Q.19 Write a program that inputs marks of N students and finds out the grade of each student. The criteria to find out the grade is as under:
// If marks are 90 or above, then grade is A+
// If marks are between 80 and 89, then grade is A
// If marks are between 60 and 79, then grade is B
// If marks are between 40 and 59, then grade is C
// If marks are below 40, then grade is F

#include<iostream.h>
#include<conio.h>
main()
{
int marks[10], i, u;
clrscr();
i = 0;
while(i<=9)
{
cout<<"Enter the marks of student no "<<i+1<<"? ";
cin>>marks[i];
i++;
}


for(i= 0;i<=9;i++)
{
cout<<endl<<"Marks of student no. "<<i+1<< " is: "<<marks[i]<<", and his grade is : " ;
if(marks[i] >= 90)
cout<<"A+";
else if(marks[i] >= 80)
cout<<"A";
else if(marks[i] >= 60)
cout<<"B";
else if(marks[i] >= 40)
cout<<"C";
else
cout<<"F";
}
getch();
}

Leave a Reply