Array Programming Exercise P-7

Array Exercise

Programming Exercise

Array Programming Exercise P-6

Programming Exercise Looping Structure P-12

Q.26 Write a program that inputs values in an array and finds out the largest value from the array using a function.

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

max = largest(arr);
cout<<"Largest value entered in the array : "<<max;
getch();
}
// definition of largest() function

long largest(long x[])
{
int mx = x[0];
for(int i = 1; i<=4; i++)
if (mx < x[i])
mx = x[i];
return mx;
}

Q.27 Write a function, and a program to test it, that will place a zero in each element of array passed to it from the calling program.

#include<iostream.h>
#include<conio.h>
main()
{
int arr[5] = {4,6,3,4,7}, i;
void zero_val(int []);
clrscr();
cout<<"Values of array before function call\n";
for(i = 0; i<=4; i++)
cout<<arr[i]<<endl;

zero_val(arr);

cout<<endl<<"Values of array after function call\n";
for(i = 0; i<=4; i++)
cout<<arr[i]<<endl;

getch();
}
// definition of zero_val() function

void zero_val(int x[])
{
for(int i = 0; i<=4; i++)
x[i] = 0;
}

Q.28 Write a program that inputs integer values in an array of 10 elements in main() function and passes array to a function. The function counts odd integer values in the array and returns the result to main() function.

#include<iostream.h>
#include<conio.h>
main()
{
int arr[10], i;
int count_odd (int []);
clrscr();
cout<<"Enter ten integer values in array\n";
for(i = 0; i<=9; i++)
{
cout<<"Enter value in element "<<i<<" ? ";
cin>>arr[i];
}

cout<<"Total odd values in the array are : "<<count_odd(arr);
getch();
}
// definition of count_odd() function

int count_odd(int x[])
{
int n = 0;
for(int i = 0; i<=9; i++)
if(x[i]%2 == 1)
n++;
return n;
}

Q.29 Write a program that sorts an array in ascending order by passing the array to a function.

#include<iostream.h>
#include<conio.h>
main()
{
int arr[10], i;
void sort(int []);
clrscr();
cout<<"Enter ten integer values in array\n";
for(i = 0; i<=9; i++)
{
cout<<"Enter value in element "<<i<<" ? ";
cin>>arr[i];
}

sort(arr);
cout<<"\nValues of array after sorting\n";
for(i = 0; i<=9; i++)
cout<<arr[i]<<" ";
getch();

}
// definition of sort() function

void sort(int x[])
{

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

Q.30 Write a program to enter 10 integers in a single dimensional array and then print out the array in descending order.

#include<iostream.h>
#include<conio.h>
main()
{
int arr[10], i;
void sort(int []);
clrscr();
cout<<"Enter ten integer values in array\n";
for(i = 0; i<=9; i++)
{
cout<<"Enter value in element "<<i<<" ? ";
cin>>arr[i];
}

sort(arr);
cout<<"\nValues of array after sorting\n";
for(i = 0; i<=9; i++)
cout<<arr[i]<<" ";
getch();

}
// definition of sort() function

void sort(int x[])
{

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

Q.31 Write a program that inputs the names of students of a class and display them in ascending and descending order.

#include<iostream.h>
#include<conio.h>
#include<string.h>
main()
{
int i;
char names[10][20];

void sort_asc(char [10][20]);
void sort_des(char [10][20]);
clrscr();
for(i = 0; i<=9; i++)
{
cout<<"Enter value of student no. "<<i+1<<" ? ";
cin>>names[i];
}

sort_asc(names);
cout<<"\nStudent names in ascending order\n";
for(i = 0; i<=9; i++)
cout<<names[i]<<endl;

sort_des(names);
cout<<"\nStudent names in descending order\n";
for(i = 0; i<=9; i++)
cout<<names[i]<<endl;
getch();

}
// definition of sort_asc() function

void sort_asc(char nm[10][20])
{

int i, u;
char temp[20];
u = 9;
while(u>=1)
{
i = 0;
while(i<u)
{
if(strcmp(nm[i],nm[i+1]) > 0)
{
strcpy(temp, nm[i]);
strcpy(nm[i], nm[i+1]);
strcpy(nm[i+1], temp);
}
i++;
}
u--;
}
}


// definition of sort_des() function

void sort_des(char nm[10][20])
{

int i, u;
char temp[20];
u = 9;
while(u>=1)
{
i = 0;
while(i<u)
{
if(strcmp(nm[i],nm[i+1]) < 0)
{
strcpy(temp, nm[i]);
strcpy(nm[i], nm[i+1]);
strcpy(nm[i+1], temp);
}
i++;
}
u--;
}
}

 

Leave a Reply