C exercises and solutions: C array


C array exercises

1. Write a program that will prompt the user to input ten integer values. The program will display the smallest and greatest of those values. It also displays the value that occur the most.


Solution:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
  int arr[10];
  int mode[10][2];
 printf("Enter 10 integer values separated by space:");
 scanf("%d %d %d %d %d %d %d %d %d %d",&arr[0], &arr[1], &arr[2], &arr[3], &arr[4], &arr[5], &arr[6], &arr[7],&arr[8],&arr[9]);
 
 
 //find the max value and min value
 int i,j,temp;
//sort array to find max and min values
for(i=0;i<10;i++)
   for(j=9;j>i;j--)
     if(arr[j]<arr[j-1]) {
          int temp=arr[j];
          arr[j]=arr[j-1];
          arr[j-1]=temp;
 }                   
                  
  printf("Max=%d,Min=%d",arr[9],arr[0]);
  printf("\n");
//initialize 2D array storing numbers of occurences, and values
 for(i=0;i<2;i++)
   for(j=0;j<10;j++)mode[j][i]=0;
     mode[0][0]=1;
 
//find mode
 for(i=0;i<10;i++)
  for(j=0;j<10;j++)
   if(arr[i]==arr[j+1]) {++mode[i][0];mode[i][1]=arr[i];}
  
//find max occurence
int max;
int k=0;
max=mode[0][0];
   for(j=0;j<10;j++)
    if(max<mode[j][0]){max=mode[j][0];k=j;}
   
//print result
  printf("The most occuring item:%d",mode[k][1]);printf("\n");
  printf("It occurs %d",max); printf("times.");
  printf("\n");
  system("PAUSE");
 
  return EXIT_SUCCESS;
}

2. Write a program to sort 10 integer values (reading from keyboard) in ascending and descending order.


      See solution 1


Comments

bhumong comment

 bhumong

solution 2

#include<stdio.h>
#include<stdlib.h>

int arr[10],swap,jumlah;

void asc(int x)
{
int y,min;
for(x=0;x<9;x++)
{
min=x;

for(y= x+1; y<10; y++)
{if(arr[min]>arr[y])
min=y;
}
if(min != x)
{swap=arr[x];
arr[x]=arr[min];
arr[min]=swap;
}
}
}

void des(int a)
{
int b,max;
for(a=0;a<9;a++)
{
max=a;

for(b= a+1; b<10; b++)
{if(arr[max]<arr[b])
max=b;
}
if(max != a)
{swap=arr[a];
arr[a]=arr[max];
arr[max]=swap;
}
}
}



int main (int argc,char *argv[])
{
int a;
printf("input 10 data\n\n");

for(jumlah = 0 ; jumlah<10 ; jumlah++)
{printf("data %d = ",jumlah+1);scanf("%d",&arr[jumlah]);
}

asc(arr[jumlah]);


printf("\nSorted list in ascending order : \n");
for(jumlah=0;jumlah<10;jumlah++)
printf("%d\n", arr[jumlah]);

des(arr[jumlah]);
printf("\nSorted list in descending order : \n");
for(jumlah=0;jumlah<10;jumlah++)
printf("%d\n", arr[jumlah]);

system("PAUSE");
return EXIT_SUCCESS;
}


2015-05-16



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.