0% found this document useful (0 votes)
9 views6 pages

Exercise Solution

Solution

Uploaded by

Nfor Lucy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Exercise Solution

Solution

Uploaded by

Nfor Lucy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PRODUCED BY AMASIA STUDENTS From Ekounou.

#include<stdio.h>
#include<conio.h>
int MAX_LENGTH = 10;
int Tab[10];
int ch;
int initialiseTab(int Tab[MAX_LENGTH]);
int sortAsc(int Tab[MAX_LENGTH]);
int sortDesc(int Tab[MAX_LENGTH]);
int sum(int Tab[MAX_LENGTH]);
int rev(int Tab[MAX_LENGTH]);
int search(int Tab[MAX_LENGTH]);
int display(int Tab[MAX_LENGTH]);
int main(){
int i;
begin:
printf("---------------------------------------------\n");
printf("Enter 10 elements in the Array:\n");
for( i = 0; i < MAX_LENGTH; i++){
scanf("%d",&Tab[i]);
}
printf("***Hello dear user let`s play with Arrays****\n");
printf("---------------------------------------------\n");
printf("1. Sort the Array in Ascending Order.\n");
printf("2. Sort the Array in Decending Order.\n");
printf("3. Sum All the elements in the Array.\n");
printf("4. Reverse the elements in the Array.\n");
printf("5. Search an Element in the Array .\n");
printf("6. Display the elements of the Array.\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch){
case 1:
sortAsc(Tab);
break;
case 2:
sortDesc(Tab);
break;
case 3:
sum(Tab);
break;
case 4:
rev(Tab);
break;
case 5:
search(Tab);
break;
case 6:
display(Tab);
break;
default:
printf("Invalid input");
}
printf("\n\n");
goto begin;
}
int display(int Tab[MAX_LENGTH]){
int i;
printf("The elements are :\n");
for( i = 0; i < MAX_LENGTH; i++){
printf("%d ",Tab[i]);
}
}
int sortAsc(int Tab[MAX_LENGTH]){
int i,j,temp;
for ( i = 0;i < MAX_LENGTH; i++){
for ( j = i + 1; j < MAX_LENGTH; j++){
if( Tab[i] > Tab[j]){
temp=Tab[i];
Tab[i]=Tab[j];
Tab[j]=temp;
}
}
}
for( i = 0; i < MAX_LENGTH; i++){
printf("%d ",Tab[i]);
}
}
int sortDesc(int Tab[MAX_LENGTH]){
int i,j,temp;
for ( i = 0;i < MAX_LENGTH; i++){
for ( j = i + 1; j < MAX_LENGTH; j++){
if( Tab[i] < Tab[j]){
temp=Tab[i];
Tab[i]=Tab[j];
Tab[j]=temp;
}
}
}
for( i = 0; i < MAX_LENGTH; i++){
printf("%d ",Tab[i]);
}
}
int sum(int Tab[MAX_LENGTH]){
int sum=0;
int i;
for( i = 0; i < MAX_LENGTH; i++){
sum = sum + Tab[i];
}
printf("The sum of all the elements of this Array is : \n");
printf("%d",sum);
}
int rev(int Tab[MAX_LENGTH]){
int i,j;
printf("The reverse of the elements entered is \n");
for (i = MAX_LENGTH; i >= 0; i--){
printf("%d ",Tab[i]);
}
}
int search(int Tab[MAX_LENGTH]){
int s,i;
printf("Enter the element to be search:\n");
scanf("%d",&s);
for( i = 0; i < MAX_LENGTH; i++){
if(s==Tab[i]){
printf(" %d is at position %d.\n ",s,i+1);
}
}
}

You might also like