0% found this document useful (0 votes)
56 views

Exercise1:: / Program That Reads 10 Integers and Prints Them in Reverse Order

The document contains 13 coding exercises involving arrays and functions in C++. Exercise 1 reads 10 integers from user input and prints them in reverse order. Exercise 2 calculates the total price of 4 items by multiplying the quantity input by the price in an array. Exercise 3 checks if student scores over 4 inputs are above or below 60, printing passed or failed.

Uploaded by

BASIC
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)
56 views

Exercise1:: / Program That Reads 10 Integers and Prints Them in Reverse Order

The document contains 13 coding exercises involving arrays and functions in C++. Exercise 1 reads 10 integers from user input and prints them in reverse order. Exercise 2 calculates the total price of 4 items by multiplying the quantity input by the price in an array. Exercise 3 checks if student scores over 4 inputs are above or below 60, printing passed or failed.

Uploaded by

BASIC
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
You are on page 1/ 16

Exercise1:

/* program that reads 10 integers and prints them in reverse order */

#include<iostream>

using namespace std;

int main()

int number [10], i;

cout<<"Program that reads 10 integers and prints them in reverse order\n";

for (i=0; i<=9; i++)

cout<<"Enter the "<<i+1<< " integer number x["<<i<<"]= ";

cin >> number[i];

cout<<endl;

cout<< "The inverse order of them is :\n";

for (i=9; i>=0; i--)

cout << "Y["<<9-i<<"]="<<number[i]<<endl;

return(0);

Exercise2:

/* Program that calculates the price of four items */

#include<iostream>
using namespace std;

int main()

int i, quantity;

float total=0, price[4]={5, 3.5, 2.50, 7.75};

cout<<"Program that calculates the price of four items :"<<endl;

for (i=0; i<=3; i++)

cout << "Enter the quantity of item " << i+1 << " :";

cin >> quantity; /*read quantity of item i */

total+=quantity*price[i];

cout<<endl;

cout << "The total cost is "<<total <<endl;

return 0;

Exercise3:

#include <iostream>

using namespace std;

int main()

int i, score[4];

cout<< "Enter 4 scores:\n";

for (i=0; i<4; i++)


{

cin >> score[i];

if (score[i] >= 60)

cout << "Student in position " << i+1 <<" passed\n";

else

cout << "Student in position " << i+1 <<" failed\n";

return 0;

Exercise4:

#include <iostream>

using namespace std;

int main()

int i,T, my_list[4], max=0, min=100;

cout<< "Enter 5 positives integers numbers between 0 and 100:\n";

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

cout<<"Enter the " <<i+1<< " integer number:";

cin>>my_list[i];

T=my_list[i];

if (T>=max)

max=T;}

if (T <= min)
min=T;

cout<< "The largest number entered is: " <<max<<endl;

cout<<"The smallest number entered is : "<<min<<endl;

return 0;

Exercise5:

/*Program that you put an index in the i th position of an array*/

#include<iostream>

using namespace std;

const int N=10;

int main()

int t[N],i,index,V;

for(i=0;i<N;i++)

cout<<"Enter an integer number t["<<i<<"]=";

cin>>t[i];

cout<<"Enter an index (between 0 and 9) : ";

cin>>index;

cout<<"Enter the value of your index V= ";

cin>>V;

if(index>=0 && index <=N-1)


{

for(i=N-1;i>index;i--)

t[i]=t[i-1];

t[index]=V;

for(i=0;i<N;i++)

cout<<"t["<<i<<"]="<<t[i]<<endl;

return 0;

Exercise6:

#include<iostream>

using namespace std;

int give_bonus(int old_score);

main()

int score[4], number;

cout << "Enter 4 scores: \n";

for (number = 0; number < 4; number++)

cin>> score[number];

for (number = 0; number < 4; number++)

score[number] = give_bonus(score[number]);

cout<< "New Scores: \n";

for (number = 0; number < 4; number++)


cout<< score[number]<< endl;

return 0;

int give_bonus(int old_score)

return (old_score+5);

Exercise7:

/* This program sorts numbers from lowest to highest of 10 entries using array*/

#include<iostream>

using namespace std;

const int N=10;

int main()

int a[N],i,nb,tmp;

for(i=0;i<N;i++)

cout<<"Enter an integer in the position "<<i<<" : ";

cin>>a[i];

do

nb=0;

for(i=0;i<N-1;i++)

if(a[i]>a[i+1])
{

tmp=a[i];

a[i]=a[i+1];

a[i+1]=tmp;

nb++;

while(nb!=0);

cout<<"the ordering array is :"<<endl;

for(i=0;i<N;i++)

cout<<"a["<<i<<"]="<<a[i]<<endl;

return 0;

Exercise8:

/*Program that give the average and the number of students have a grade more than the
average of 10 students*/

#include <iostream>

using namespace std ;

int main()

int i, nbm ;

float average, sum ;

float t[10] ;

for (i=0 ; i<10 ; i++)

cout << "Please the grade of student number " << i+1 << " : " ;
cin >> t[i] ;

for (i=0, sum=0 ; i<10 ; i++)

sum += t[i] ;

average = sum / 10 ;

cout << "\ The average of the class is : " << average << "\n" ;

for (i=0, nbm=0 ; i<10 ; i++ )

if (t[i] > average)

nbm++ ;

cout << nbm << " students have a grade more than the average" ;

return 0;

Exercise9:

/*Program that search an entry number between 0 to 10 for n entries*/

#include<iostream>

using namespace std;

void enter(int t[],int n) /* function to enter n entries */

int i; for(i=0;i<n;i++)

cout<<"Please, enter the value of the position "<<i<<" : ";

cin>> t[i];

}
bool f(int t[], int n, int &v) /*function to check if there exists an entry between 0 to 10*/

bool found=false;

int i=0;

while(!found && i<n)

if(t[i]>=0 && t[i]<=10)

found=true;

v=t[i];

else i++;

return found;

int main()

int m;

cout<<"How many entries ? n=:";

cin>>m;

int a[m];

bool b;

int w;

enter(a,m);

b=f(a,m,w);

if (b)

cout<<"\nThere exists a value between 0 and 10 : "<<w<<" is the first of these values."<<endl;
else

cout<<"There doest not exist between 0 and 10"<<endl;

return 0;

Exercise10:

/*Program that to print an entries numbers between 0 to 10 for n entries*/

#include<iostream>

using namespace std;

void input(int t[],int n) /* function to input n entries*/

int i;

for(i=0;i<n;i++)

cout<<"Enter the value number "<<i<<" : ";

cin>> t[i];

void print(int t[],int n) /* function to print*/

int i;

for(i=0;i<n;i++)

cout<<t[i]<<" ";

cout<<endl;

int f(int t1[], int n,int t2[]) /* function to search entries number between 0 to 10*/
{

int i=0,nb=0;

for(i=0;i<n;i++)

if(t1[i]>=0 && t1[i]<=10)

{t2[nb]=t1[i];

nb++;}

return nb;

int main()

int m;

cout<<"How many entries ? n=:";

cin>>m;

int a[m],b[m];

int nb;

input(a,m);

nb=f(a,m,b);

cout<<"This is the values between 0 and 10 : "<<endl;

print(b,nb);

return 0;

Exercise11:

/* Program input: Enter the coefficients of a squared matrix of size 4 and output give the sum of
its coefficients*/

#include<iostream>

using namespace std;


int main()

int a[4][4],i,j,s=0;

cout<<"Enter the elements of the matrix"<<endl;

for(i=0; i<4; i++)

for(j=0; j<4; j++)

cin>>a[i][j];

cout<<"The sum of the elements of the matrix=";

for(i=0; i<4; i++)

for(j=0; j<4; j++)

s=s+a[i][j];

cout<<s;

return 0;

Exercise12:

/* program addition of two squared matrices of size 3 */

#include<iostream>

#include<conio.h>

using namespace std;

int main()

int x[3][3],y[3][3],z[3][3],i,j;

cout<<"Enter the coefficient of your first matrix :"<<endl;

for (i=0;i<3;i++)
for(j=0;j<3;j++)

cin>>x[i][j];

cout<<"Enter the coefficient of your second matrix :"<<endl;

for (i=0;i<3;i++)

for(j=0;j<3;j++)

cin>>y[i][j];

cout<<"Matrix [X]=";

cout<<"(";

for(i=0;i<3;i++)

cout<<"\n\n";

for(j=0;j<3;j++)

cout<<x[i][j]<<"\t";

cout<<")"<<endl;

cout<<"\nMatrix [Y]=";

for(i=0;i<3;i++)

cout<<"\n\n";

for(j=0;j<3;j++)

cout<<y[i][j]<<"\t";

}
for(i=0;i<3;i++)

for(j=0;j<3;j++)

z[i][j]=x[i][j]+y[i][j];

cout<<"\n Their sum is the matrix [Z]=";

for(i=0;i<3;i++)

cout<<"\n\n";

for(j=0;j<3;j++)

cout<<z[i][j]<<"\t";

return 0;

Exercise13:

#include<iostream>

using namespace std;

class student

private:

int id;

char name[20];

char grade;

public:

student(); /*student(int the_id);*/


int get_id();

void get_name();

void get_grade();

void output();

};

student::student()

id=0;

int student::get_id()

cout<<"Enter the Id of student:";

cin>>id;

return id;

void student::get_name()

cout<<"Enter the name of student:";

cin>>name;

void student::get_grade()
{

cout<<"Enter the grade of the student:";

cin>>grade;

void student::output()

cout << "Student details:\n";

cout << "Id Number:"<< id << ", name of student:" << name << ", grade:" << grade;

int main()

student stud;

int a;

a=stud.get_id();

stud.get_name();

stud.get_grade();

cout<<endl;

stud.output();

return 0;

You might also like