C++ exercises and solutions: C++ arrays


C++ arrays

1. Write a C++ 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 occurs the most.

Solution:


#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
  int arr[10];
  int mode[10][2];
   cout<<"Enter 10 integer values\n";
  for(int l=0;l<10;l++) {

     cout<<"value "<<l<<":";cin>>a[l]; 

} 
 //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;
}
                 

  cout<<"Max="<<arr[9]<<"\nMin="<<arr[0];
  cout<<"\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
  cout<<"The most occurring item:"<<mode[k][1]<<"\n";
  cout<<"It occurs "<<max<<" times.";
  cout<<"\n";
  system("PAUSE");
 
  return EXIT_SUCCESS;
}

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

  See solution 1


Comments

king comment

 king

Write a program that defines an array of 10 integers ranging from 1 to 100. Prompt the user to enter 10 integers and store in an array. Search the array for the highest and lowest scores and print both of these values together with it index


2018-04-04
ana comment

 ana

Write a program that will read in N numbers, each of which is between 1 and 100, inclusive. As each number is read, print it only if it is not a duplicate of a number already read.


2018-01-16
devanshi solanki comment

 devanshi solanki

write a program to convert entire string into upper case & vice versa.


2017-08-26
devanshi solanki comment

 devanshi solanki

write a program to convert entire string into upper case & vice versa.


2017-08-26
jad comment

 jad

Write a program in c++ that ask the user to type 10 integers of an array and an integer value X, the program must put the value X in the index 0, shifting each element right and dropping of the last element. The program must write the array before and after shifting ?


2017-03-22
Ashot comment

 Ashot

#include <iostream>
using namespace std;
int gcd(int a,int b)
{
int m,n;
m=a;
n=b;
while(m!=n)
if(m<n) m=m+a;
else n=n+b;
return m;
}
int main()
{
int n,k;
cin>>n>>k;
cout<<gcd(n,k)<<endl;
return 0;
}


2017-01-13
Ashot comment

 Ashot

//a program that find the greatest common divisor (GCD) of numbers n and k:
#include <iostream>
using namespace std;
int gcd(int a,int b)
{
int m,n;
m=a;
n=b;
while(m!=n)
if(m<n) m=m+a;
else n=n+b;
return m;
}
int main()
{
int n,k;
cin>>n>>k;
cout<<gcd(n,k)<<endl;
return 0;
}


2017-01-13
Talha Moazzam comment

 Talha Moazzam

write a program that take input of age & marks of candidates only store the record of those candidates whose age is less than 20 also display the candidate's serial number where total number of candidates are 5 using Arrays in C++

Code:

#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
system ("color 4F");
cout << "\t\t\tTotal Number Candidates Are 5" << endl;
int age[5];
cout << endl;
for (int i=1;i<=5;i++)
{
cout << "Enter The " << i << " Candidate Age: ";
cin >> age[i];

}
int marks[5];
cout << endl;
for (int j=1;j<=5;j++)
{
cout << "Enter The " << j << " Candidate Marks: ";
cin >> marks[j];
}
string serialNo[100];
cout << endl;
for (int k=1;k<=5;k++)
{
cout << "Enter The " << k << " Candidate Serial Number: ";
cin >> serialNo[k];
}
cout << endl;
for (int l=1;l<=5;l++)
{
if (age[l]<20)
{
cout << "Age: " << age[l] << " " << endl;
cout << "Serial Number: " << serialNo[l] << " " << endl;
cout << "Marks: " << marks[l] << " " << endl;
}
cout << endl;
}
}


2017-01-06
Talha Moazzam comment

 Talha Moazzam

write a program that take input of age & marks of candidates only store the record of those candidates whose age is less than 20 also display the candidate's serial number where total number of candidates are 5 using Arrays in C++

Code:

#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
system ("color 4F");
cout << "\t\t\tTotal Number Candidates Are 5" << endl;
int age[5];
cout << endl;
for (int i=1;i<=5;i++)
{
cout << "Enter The " << i << " Candidate Age: ";
cin >> age[i];

}
int marks[5];
cout << endl;
for (int j=1;j<=5;j++)
{
cout << "Enter The " << j << " Candidate Marks: ";
cin >> marks[j];
}
string serialNo[100];
cout << endl;
for (int k=1;k<=5;k++)
{
cout << "Enter The " << k << " Candidate Serial Number: ";
cin >> serialNo[k];
}
cout << endl;
for (int l=1;l<=5;l++)
{
if (age[l]<20)
{
cout << "Age: " << age[l] << " " << endl;
cout << "Serial Number: " << serialNo[l] << " " << endl;
cout << "Marks: " << marks[l] << " " << endl;
}
cout << endl;
}
}


2017-01-06
Jamisyed@facebook.com comment

 Jamisyed@facebook.com

#include <iostream.h>
main()
{
int i=0;
int ageTemp=0;
int marksTemp=0;
int ageRis=20;
int candidateNo=10;
int age[candidateNo];
int marks[candidateNo];
char name[candidateNo];
do

{
cout << " Enter Your Age = ";
cin >> ageTemp;



if(ageTemp<=ageRis)


{
cout << " Enter Your Marks = ";
cin >> marksTemp;
ageTemp=age[i];
marksTemp=marks[i];
i++;
}

else
{
cout << " Sory Your Age is less Then 20 " << "\n";

}

}

while (i<=candidateNo);


system ("pause");

}


2016-12-08
Jamisyed@facebook.com comment

 Jamisyed@facebook.com

#include <iostream.h>
main()
{
int i=0;
int ageTemp=0;
int marksTemp=0;
int ageRis=20;
int candidateNo=10;
int age[candidateNo];
int marks[candidateNo];
char name[candidateNo];
do

{
cout << " Enter Your Age = ";
cin >> ageTemp;



if(ageTemp<=ageRis)


{
cout << " Enter Your Marks = ";
cin >> marksTemp;
ageTemp=age[i];
marksTemp=marks[i];
i++;
}

else
{
cout << " Sory Your Age is less Then 20 " << "\n";

}

}

while (i<=candidateNo);


system ("pause");

}


2016-12-08
hamza ali comment

 hamza ali

plz give the solution of this

Write a program that incorporates functions that insert values in an array and delete values in the array. Insert function should have three arguments: Array, Index and Data, while the Delete function should have two arguments: Array and Index.


2016-11-23
I

 I'm better

Much simpler solution which is actually readable...

#include<iostream>
using namespace std;
int main(){
int number[5] = {};
int max = 0;
int min = 0;
int max2 = 0;
int shit;
int someshit[5] = {};

cout << "Input 5 numbers\n";

for (int i = 0; i < 5; ++i)
{
cin >> number[i];
}

max = number[0];
min = number[0];

for (int i = 0; i < 5; ++i)
{
if (max < number[i])
{
max = number[i];
}
}

for (int i = 0; i < 5; ++i)
{
if (min > number[i])
{
min = number[i];
}
}

int k = 0;

for (int i = 0; i < 5; ++i)
{

for (int j = 0; j < 5; ++j)
{
if (number[i] == number[j])
{
someshit[i]++;
}
}
}

max2 = someshit[0];
for (int i = 0; i < 5; ++i)
{
if (max2 < someshit[i])
{
max2 = someshit[i];
k = i;
}
}
cout << number[k]<< " occured the most.";
cout << "It occured " << max2 << " times" << endl;
cout << "\n\nHighest Number\n";
cout << max << endl;
cout << "Lowest Number\n";
cout << min << endl;
system("pause");}


2016-10-05
KittenPaws comment

 KittenPaws

I used the following code for finding and printing maximum occurences to show if multiple numbers occur the same time:
"
//initialize 2D array storing numbers of occurences, and values
int j;
for (i = 0;i < 10;i++)
{
mode[i][1] = 0;
mode[i][0] = arr[i];
}
//counting number of occurences
for (i = 0;i<10;i++)
for (j = 0;j<10;j++)
if (arr[j] == mode[i][0]) ++mode[i][1];

//find max occurence
int occ[10];
int k = 0,t=0;
for (j = 0;j < 10;j++)
if (k < mode[j][1])k = mode[j][1] ;
//storing the most occuring numbers
for (j=0;j<10;j++)
if (k == mode[j][1])
{
occ[t] = mode[j][0];
t++;
};
//deleting duplicates
for (i = 0;i < 10;i++)
for (j = i+1;j < 10;j++)
if (occ[i] == occ[j]) occ[j] = NULL;
//print result
cout << "The most occurring item(s) occur " << k << " times\n";
cout << "They are:\n";
for (i = 0;i < 10;i++)
if (occ[i] != NULL) cout << occ[i] << "\n";
system("PAUSE");


2016-09-26
Meena Rajpoot  comment

 Meena Rajpoot

hi ! help me to
write a program that take input of age & marks of candidates only store the record of those candidates whose age is less than 20 also display the candidate's serial number where total number of candidates are 5 using Arrays in C++


2016-08-30
khaled awad comment

 khaled awad

how to write a program that reads two numbers a and b, then it calculates their greater common divisor (GCD).
using the function :bool DvsCom(int n,int a, int b)//to determine if an integer n is a common divisor for a and b


2016-07-09
GM comment

 GM

Program 1.
ERROR IN LINE 9.
cin>>a[l]; istead of cin>>arr[l];


2016-04-09
GM comment

 GM

ERROR IN LINE 9.
cin>>a[l]; istead of cin>>arr[l];


2016-04-09
C++ comment

 C++

Hello


2015-12-11
Michael comment

 Michael

how to return no. of days according to its month and accepts an unsigned integer n. Ex. if n = 6 the return value is 30 because the month of june has 30 days.(using C++)


2015-08-03
Michael comment

 Michael

how to return no. of days according to its month and accepts an unsigned integer n. Ex. if n = 6 the return value is 30 because the month of june has 30 days.(using C++)


2015-08-03
Malaysiandude comment

 Malaysiandude

To the people who copy and paste this code on your compiler,there is a bug on line 11.

Please change it to the line below:

cout<<"value "<<l<<":";cin>>arr[l];


2015-08-03
Dara comment

 Dara

It is simple. After you sort the array, you will be able to find the max and min values in the array.


2014-05-05
beginner comment

 beginner

Hi i can"t understand the solution you gave as from this part: //find the max value and min value .
can anyone help me please? thanks


2014-05-04



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.