3. Program that consists of four arrays representing numbers, thier squares, their cubes and sums array stores the corresponding sums of three arrays each consisting of 10 elements.
Chapter 7:
Arrays
Programming Exercise
Problem # 3:
Write a program that four arrays numbers, squares, cubes and sums each consisting of 10 elements. The numbers array stores the values of its indexes, the squares array stores the squares of its indexes, the cubes array stores the cubes of its indexes and sums array stores the corresponding indexes of three arrays. The program should displays the values of sums array and the total of all values in sums array.
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
int i;
int num[10], squares[10], cubes[10], sums[10], t_sum=0;
for(i=0;i<10;i++)
{
num[i]=i;
squares[i]=i*i;
cubes[i]=i*i*i;
sums[i]=num[i]+squares[i]+cubes[i];
t_sum= t_sum + sums[i];
}
cout<<"Numbers\tSquares\tCubes\tsums"<<endl;
for(i=0;i<10;i++)
{
cout<<num[i]<<"\t"<<squares[i]<<"\t"<<cubes[i]<<"\t"<<sums[i]<<endl;
}
cout<<"Grand total: "<<t_sum;
}
#include<conio.h>
using namespace std;
int main ()
{
int i;
int num[10], squares[10], cubes[10], sums[10], t_sum=0;
for(i=0;i<10;i++)
{
num[i]=i;
squares[i]=i*i;
cubes[i]=i*i*i;
sums[i]=num[i]+squares[i]+cubes[i];
t_sum= t_sum + sums[i];
}
cout<<"Numbers\tSquares\tCubes\tsums"<<endl;
for(i=0;i<10;i++)
{
cout<<num[i]<<"\t"<<squares[i]<<"\t"<<cubes[i]<<"\t"<<sums[i]<<endl;
}
cout<<"Grand total: "<<t_sum;
}
Output:
Let me know in the comment section if you have any question.
Previous Post:
2. Program that uses two arrays to store the roll # and marks of student. It inputs roll # and marks of five students, displays the roll # and marks of the student with highest marks.
Comments
Post a Comment