Array Programming Exercise P-6

Array Exercise

Programming Exercise

Array Programming Exercise P-5

Programming Exercise Looping Structure P-12

Q.20 Write a program that sorts an array ‘ABC’ in ascending order using Bubble Sort method. The array consists of N elements.

#include<iostream.h>
#include<conio.h>
main()
{
int ABC[10], i, u, temp;
clrscr();
i = 0;
while(i<=9)
{
cout<<"Enter value for element "<<i<<"? ";
cin>>ABC[i];
i++;
}

u = 9;
while(u>=1)
{
i = 0;
while(i<u)
{
if(ABC[i] > ABC[i+1])
{
temp = ABC[i];
ABC[i] = ABC[i+1];
ABC[i+1] = temp;
}
i++;
}
u--;
}

cout<<"\nValues of array ABC in ascending order\n";
for(i = 0; i<=9; i++)
cout<<ABC[i]<<" ";
getch();
}

Q.21 Write a program that uses three arrays ‘roll_no’, ‘name’ and ‘marks’. It inputs roll numbers, names and marks of 10 students into these arrays. It searches the highest marks and displays detail of the student that has highest marks such as its roll number, name, and marks. For example, if roll number is 1 and is stored in roll_no[0], then its name and marks must be stored in name[0] and marks[0] respectively.

#include<iostream.h>
#include<conio.h>
main()
{
int roll_no[10], marks[10], i, pos, highest_marks;
char names[10][25];
clrscr();
i = 0;
while(i<=9)
{
cout<<endl<<"Enter record of student : "<<i+1<<endl;
cout<<"Roll number : ";
cin>>roll_no[i];
cout<<"Name : ";
cin>>names[i];
cout<<"Marks : ";
cin>>marks[i];
i++;
}

highest_marks = marks[0];
i = 1;
while(i<=9)
{
if(marks[i] > highest_marks)
{
highest_marks = marks[i];
pos = i;
}
i++;
}

cout<<"\nDetail of student that has highest marks\n";
cout<<"Roll number : "<<roll_no[pos]<<endl;
cout<<"Name : "<<names[pos]<<endl;
cout<<"Marks : "<<marks[pos]<<endl;
getch();
}

Q.22 Write a program that uses three arrays ‘A’, ‘B’, and ‘C’. It inputs integer values in array ‘A’. In second array ‘B’, it stores the squares of values of array ‘A’ and in third array ‘C’, it stores cubes of the values of ‘A’. The program should display the contents of three arrays, i.e. ‘A’, ‘B’, and ‘C’.
// Suppose array ‘A’ has values: 2, 3, 5, 6, 7
// The values of ‘B’ should be: 4, 9, 25, 36, 49
// The values of ‘C’ should be: 8, 27, 125, 216, 343

#include<iostream.h>
#include<conio.h>
main()
{
long A[5], B[5], C[5], i;
clrscr();
i = 0;
while(i<=4)
{
cout<<"Enter value in element "<<i<< " of array A ? ";
cin>>A[i];
B[i] = A[i] * A[i];
C[i] = A[i] * A[i] * A[i];
i++;
}

cout<<"Array A \t Array B \t Array C "<<endl;
i = 0;
while(i<=4)
{
cout<<A[i]<<'\t'<<B[i]<<'\t'<<C[i]<<endl; 
i++;
}
getch();
}

Q.23 Write a program that inputs employees code, names, and salaries of 10 employees in three arrays ‘code’, ‘name’, and ‘salary’ It computes the net salary. If the salary is 250000 or more, it deducts 13% of the salary. If the salary is 150000 or more but less than 250000, it deducts 1500 from the salary.

#include<iostream.h>
#include<conio.h>
main()
{
int emp_code[10], i;
float emp_salary[10], net_salary;
char emp_names[10][25];
clrscr();
i = 0;
while(i<=9)
{
cout<<endl<<"Enter record of employee : "<<i+1<<endl;
cout<<"Employee code : ";
cin>>emp_code[i];
cout<<"Employee name : ";
cin>>emp_names[i];
cout<<"Employee Salary : ";
cin>>emp_salary[i];
i++;
}

i = 0;
while(i<=9)
{
if(emp_salary[i] >=250000)
net_salary = emp_salary[i] - emp_salary[i] *10.0/100;
else if(emp_salary[i] >=150000)
net_salary = emp_salary[i] - 1500;

cout<<endl<<"Record of employee # "<<i+1<<endl;
cout<<"Employee code : "<<emp_code[i]<<endl;
cout<<"Employee name : "<<emp_names[i]<<endl;
cout<<"Employee Salary : "<<emp_salary[i]<<endl;
cout<<"Employee Net Salary : "<<net_salary;
cout<<endl;
i++;
}
getch();
}

Q.24 Write a program that inputs values into a table and sorts the values of the table row-wise. It displays the values of unsorted and sorted table on screen in tabular form.

#include<iostream.h>
#include<conio.h>
main()
{
int tab1[3][5], tab2[3][5], r, c, u, i, temp;
clrscr();
r = 0;
while(r<=2)
{
for(c = 0; c<=4; c++)
{
cout<<"Enter value for row "<<r<<" and column "<<c<<" ? ";
cin>>tab1[r][c];
tab2[r][c] = tab1[r][c];
}
r++;
}

// sort table row-wise

r = 0;
while(r<=2)
{
u = 4;
while(u>=1)
{
i = 0;
while(i<u)
{
if(tab1[r][i] > tab1[r][i+1])
{
temp = tab1[r][i];
tab1[r][i] = tab1[r][i+1];
tab1[r][i+1] = temp;
}
i++;
}
u--;
}
r++;
}


cout<<"\nValues of unsorted table row-wise in tabular form" <<endl;
for(r = 0; r<=2; r++)
{
for(c = 0; c<=4; c++)
cout<<tab2[r][c]<<"\t";
cout<<endl;
}


cout<<"\nValues of sorted table in tabular form" <<endl;
for(r = 0; r<=2; r++)
{
for(c = 0; c<=4; c++)
cout<<tab1[r][c]<<"\t";
cout<<endl;
}
getch();
}

Q.25 Write a program that initializes roll numbers and marks of different subjects in two-dimensional array. The roll numbers should be in first column and marks of different subjects should be in other columns such as:
// 1 55 78 95 75
// 2 54 85 62 85
// 3 58 81 85 75
// 4 60 82 74 75
// 5 45 72 25 74

// The program should read the 2-D array, compute the total marks of each student and store the result in the last column.

#include<iostream.h>
#include<conio.h>
main()
{
int tab1[5][6], r, c, s;
clrscr(); r = 0;
while(r<=4)
{
c = 0;
s = 0;
cout<<"Enter record # "<<r+1<<endl;
cout<<"Enter roll number ? ";
cin>>tab1[r][c];
while(c<4)
{
c = c + 1;
cout<<"Enter marks of subject "<<c<<" ? ";
cin>>tab1[r][c];
s = s + tab1[r][c];
}
c = c + 1;
tab1[r][c] = s;
r++;
}


cout<<"\nRecords of students:" <<endl;
cout<<"Roll#\tSub1\tSub2\tSub3\tSub4\tTotal Marks"<<endl;
for(r = 0; r<=4; r++)
{
for(c = 0; c<=5; c++)
cout<<tab1[r][c]<<"\t";
cout<<endl;
}

getch();
}

Leave a Reply