﻿ C++ exercises and solutions: C++ arrays

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;
int mode;
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<<"\nMin="<<arr;
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=1;

//find mode
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(arr[i]==arr[j+1]) {++mode[i];mode[i]=arr[i];}

//find max occurence
int max;
int k=0;
max=mode;
for(j=0;j<10;j++)
if(max<mode[j]){max=mode[j];k=j;}

//print result
cout<<"The most occurring item:"<<mode[k]<<"\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 kingWrite 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 anaWrite 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 solankiwrite a program to convert entire string into upper case & vice versa.2017-08-26 devanshi solankiwrite a program to convert entire string into upper case & vice versa.2017-08-26 jadWrite 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#include using namespace std; int gcd(int a,int b) { int m,n; m=a; n=b; while(m!=n) if(m>n>>k; cout< using namespace std; int gcd(int a,int b) { int m,n; m=a; n=b; while(m!=n) if(m>n>>k; cout< #include using namespace std; int main () { system ("color 4F"); cout << "\t\t\tTotal Number Candidates Are 5" << endl; int age; cout << endl; for (int i=1;i<=5;i++) { cout << "Enter The " << i << " Candidate Age: "; cin >> age[i]; } int marks; cout << endl; for (int j=1;j<=5;j++) { cout << "Enter The " << j << " Candidate Marks: "; cin >> marks[j]; } string serialNo; 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 Moazzamwrite 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 #include using namespace std; int main () { system ("color 4F"); cout << "\t\t\tTotal Number Candidates Are 5" << endl; int age; cout << endl; for (int i=1;i<=5;i++) { cout << "Enter The " << i << " Candidate Age: "; cin >> age[i]; } int marks; cout << endl; for (int j=1;j<=5;j++) { cout << "Enter The " << j << " Candidate Marks: "; cin >> marks[j]; } string serialNo; 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#include 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#include 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 aliplz 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'm betterMuch simpler solution which is actually readable... #include using namespace std; int main(){ int number = {}; int max = 0; int min = 0; int max2 = 0; int shit; int someshit = {}; cout << "Input 5 numbers\n"; for (int i = 0; i < 5; ++i) { cin >> number[i]; } max = number; min = number; 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; 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 KittenPawsI 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] = 0; mode[i] = arr[i]; } //counting number of occurences for (i = 0;i<10;i++) for (j = 0;j<10;j++) if (arr[j] == mode[i]) ++mode[i]; //find max occurence int occ; int k = 0,t=0; for (j = 0;j < 10;j++) if (k < mode[j])k = mode[j] ; //storing the most occuring numbers for (j=0;j<10;j++) if (k == mode[j]) { occ[t] = mode[j]; 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 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 awadhow 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 b2016-07-09 GMProgram 1. ERROR IN LINE 9. cin>>a[l]; istead of cin>>arr[l];2016-04-09 GMERROR IN LINE 9. cin>>a[l]; istead of cin>>arr[l];2016-04-09 C++Hello 2015-12-11 Michaelhow 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 Michaelhow 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 MalaysiandudeTo 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 "<>arr[l];2015-08-03 DaraIt is simple. After you sort the array, you will be able to find the max and min values in the array.2014-05-05 beginnerHi i can"t understand the solution you gave as from this part: //find the max value and min value . can anyone help me please? thanks2014-05-04