GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 449 of 453    Print  
Read and Sort 30 integers into One Dimension Array
Write a C programme that read 30 integer numbers into one dimension array then using a function to sort them in desinding order (this function should use another function to exchange any two elements int the array) after that print desorted elements?


  
Total Answers and Comments: 4 Last Update: September 25, 2009     Asked by: Narmeen 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: sanoopmakkar
 

 #include<stdio.h>
 #include<conio.h>
void swap(int a[],int j,int i)
{
int t;
t=a[i];
a[i]=a[j+1];
a[j+1]=t;
}


void sort(int a[])
{
int i,j;
for(i=0;i<5;i++)
for(j=i;j<4;j++)
if(a[i]<a[j+1])
swap(a,j,i);
}



void main()
{
int a[6],i,j,t;
printf("enter 5 numbersnn");
for(i=0;i<5;i++)
scanf("%d", &a[i]);
sort(a);
printf("sorted array is n");
for(i=0;i<5;i++)
printf("n%d", a[i]);
getch();
}



Above answer was rated as good by the following members:
kushks, avinashsharma128, sivaec61
August 08, 2009 04:38:26   #1  
manjunatha.dm86 Member Since: August 2009   Contribution: 3    

RE: Read and Sort 30 integers into One Dimension Array
void main()
{
int a[35] i j t;
printf("enter 30 numbersn");
for(i 0;i<30;i++)
scanf(" d" &a[i]);
for(i 0;i<30;i++)
for(j i;j<30;j++)
if(a[j]>a[j+1])
{
t a[j];
a[j] a[j+1];
a[j+1] t;
}
getch();
}

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
August 08, 2009 04:50:45   #2  
manjunatha.dm86 Member Since: August 2009   Contribution: 3    

RE: Read and Sort 30 integers into One Dimension Array
void swap(int a[] j)
{
t a[j];
a[j] a[j+1];
a[j+1] t;
}
void sort(int a[])
{
for(i 0;i<30;i++)
for(j i;j<29;j++)
if(a[j]>a[j+1])
swap(a j);
}
void main()
{
int a[35] i j t;
printf("enter 30 numbersn");
for(i 0;i<30;i++)
scanf(" d" &a[i]);
sort(a);
printf("sorted array is n");
for(i 0;i<30;i++)
printf(" dt" a[i]);
getch();
}

 
Is this answer useful? Yes | No
August 10, 2009 12:04:36   #3  
sanoopmakkar Member Since: August 2009   Contribution: 1    

RE: Read and Sort 30 integers into One Dimension Array

#include<stdio.h>
#include<conio.h>
void swap(int a[] int j int i)
{
int t;
t a[i];
a[i] a[j+1];
a[j+1] t;
}


void sort(int a[])
{
int i j;
for(i 0;i<5;i++)
for(j i;j<4;j++)
if(a[i]<a[j+1])
swap(a j i);
}



void main()
{
int a[6] i j t;
printf("enter 5 numbersnn");
for(i 0;i<5;i++)
scanf(" d" &a[i]);
sort(a);
printf("sorted array is n");
for(i 0;i<5;i++)
printf("n d" a[i]);
getch();
}


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
September 03, 2009 04:56:03   #4  
sarojkumar21.panda Member Since: September 2009   Contribution: 3    

RE: Read and Sort 30 integers into One Dimension Array

Hi
Let break the question into three parts
1-Read 3o intgers and store them in one dimensional array.
2. Write one function to sort them in descending order.Let the function name is
sort()
Let suppose there are five elements
11 23 32 12 1
For sorting you need to compare the 1st elemnt that is 11 with the rest of the elements .While comapring the 2nd elelment you found that 11 is smaller than the 23.
Then these elements need to be swapped .This swapping need to be done againg through afunction called swap()
3.swap() -this function is used to swap the element.
4.Printing should be done on the desorted array then before sorting you need to store it in another array.

Execute the following example and u will be clear.


#include<stdio.h>
void sort(int a[]);
void swap(int * int *);


int main()
{
int a[30] b[30];
int i j;
printf("eneter 10 number");
for(i 0;i<30;i++)
{
scanf(" d" &a[i]);
}
memcpy(b a sizeof(a)); /* here we have stored the original array into array b
sort(a);
printf("the number in sorted and descending order is n");
for(i 0;i<30;i++)
{
printf(" d t" a[i]);
}
printf("n");
for(i 0;i<30;i++)
{
printf(" the original array d t" b[i]);
}

return 0;
}

void sort(int arr[]) /* this function is used to sort the array */
{
int i j;
for(i 0;i<10;i++)
{
for(j i+1;j<10;j++)
{
if(arr[i]<arr[j])
{
swap(&arr[i] &arr[j]);
}
}
}
}

void swap(int *p int *q) /* This is used to swap the two elements */
{
int temp;
temp *p;
*p *q;
*q temp;
}


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape