// try out
//Program - 5.4
# include <iostream.h>
# include <conio.h>
void main ( )
{
int x[5] = {1,2,3,4,5}, y [5] = {5,4,3,2,1},
result [5] = { 0,0,0,0,0 };
int i= 0;
while (i++ < 5)
result = x - y ;
clrscr ( );
cout << “\n The contents of the array are: \n”;
i= 0 ;
do
{
cout << ‘\t’ << x
<< ‘\t’ << y
<< ‘\t’ << result <<‘\n’;
i++;
} while (i<5);
getch ( );
}


What is the output of the below program?
I want walk through of this program

// try out
//Program - 5.4
# include <iostream.h>
# include <conio.h>
void main ( )
{
int x[5] = {1,2,3,4,5}, y [5] = {5,4,3,2,1},
result [5] = { 0,0,0,0,0 };
int i= 0;
while (i++ < 5)
result [i] = x [i] - y [i];
clrscr ( );
cout << “\n The contents of the array are: \n”;
i= 0 ;
do
{
cout << ‘\t’ << x [i]
<< ‘\t’ << y [i]
<< ‘\t’ << result [i]<<‘\n’;
i++;
} while (i<5);
getch ( );
}

 
What is the output of the below program?
I want walk through of this program
int x[5] = {1,2,3,4,5}, y[5] = {5,4,3,2,1},
result[5] = {0,0,0,0,0};

This creates 3 arrays of the type int, with the names x, y and result.

int i = 0;
while(i++ < 5)
        result[i] = x[i] - y[i];

This loops through a range of 1 to 5 with i as the current value, and puts x - y into result

clrscr();

this clears the console's text buffer(get's rid of all the text in the console screen)

cout << “\n The contents of the array are: \n”;
i= 0 ;
do
{
        cout << ‘\t’ << x [i]
        << ‘\t’ << y [i]
        << ‘\t’ << result [i]<<‘\n’;
        i++;
} while (i<5);

'\n' = newline
'\t' a tab(usually equal to the width of 4 spaces)

this prints out all the contents of the arrays, one after eachother while i(which gets incremented(+1) each run through the loop) is smaller then 5.

getch();

waits for the user to input something

If anyone see's something incorrect, or they think they can offer a better explanation, feel free to correct me.

This article has been dead for over six months. Start a new discussion instead.