0% found this document useful (0 votes)
6 views4 pages

Function & Array

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)
6 views4 pages

Function & Array

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

/*Write C++ program, to uses a function called square to

calculate the number squares from 1 to 10?*/


#include <iostream>
using namespace std;
int square(int y)
{
return y*y;
}
int main(){
for(int x=1;x<=10;x++)
cout<<square(x)<<endl;
return 0 ;
}

Write a function to find the largest integer among three


integers entered by the user in the main function?
#include <iostream>
using namespace std;
int max(int y1, int y2, int y3)
{
int big;
big=y1;
if (y2>big)
big=y2;
if (y3>big)
big=y3;
return (big);
}
int main( ) {
int largest,x1,x2,x3;
cout<<"Enter 3 integer numbers:";
cin>>x1>>x2>>x3;
cout<<max(x1,x2,x3);
}

Write a function to find the average integer for three integers


entered by the user in the main function
#include <iostream>
using namespace std;
float average(int x , int y ,int z)
{
float average= (x+y+z)/3 ;
return average;
}
int main(){
int a , b ,c ;
cin>>a >>b>>c ;
cout <<average(a, b, c) ;
return 0 ;
}
//Write a program that ask the user to enter 10 Employee
salaries and store them, then add sum for salary and print out
the average salary value.
#include<iostream>
using namespace std;
int main()
{
float sum = 0;
float salar[10];
for (int i = 0; i < 10; i++)
{
cout << "enter salary " << i << endl;
cin >> salar[i];
}
for (int i = 0; i < 10; i++)
{
sum += salar[i];
}
cout << "the average salary is " << sum / 10 << endl;
}

//Write a program that build a matrix of 5 rows and 3columns. As the use
to enter the values for all the matrix items print out the sum of all
matrix items and print out the sum of the diagonal items.

#include <iostream>
using namespace std;
int main()
{

int matrix[5][3];

for(int row=0;row<5 ;row++)


for (int col = 0; col < 3; col++)
{
cout << "enter value " << row << " ," << col << endl;
cin >> matrix[row][col];
}
int sum = 0;
for (int row = 0; row < 5; row++)
for (int col = 0; col < 3; col++)
{
sum = sum + matrix[row][col];
}
int sum_diaga = 0;
for (int row = 0; row < 5; row++)
for (int col = 0; col < 3; col++)
{
if(row==col)
sum_diaga = sum_diaga + matrix[row][col];
}
cout << "the sum of all matrix is" << sum << endl;
cout << "the sum of diagonal is " << sum_diaga << endl;
}

You might also like