REVISION 9
(1)- Show the output of the following program segment:
int A [ 4 ] = {2, -1, 3, 2};
int B [ 4 ] = {1, -2, 2, -3};
int sum = 0;
for( int i = 0; i < 4; i++ )
sum += A [ i ] * B [ i ];
cout << "The sum is " << sum << endl;
(2)- Show the initial and final contents of the array “A”
int A[8] = {55,55,55,55,50,80,80,65} initial final
int T; bool cont = true;
int n= 8, j = 1; A A
while (j < 8 && cont) A[0] 55 80
{
If (A[ j ] > A[0]) A[1] 55 55
{ A[2] 55 55
T = A[0]; A[3] 55 55
A[0] = A[j]; A[4] 50 50
A[j] = T; A[5] 80 50
cont = false; A[6] 80 50
} A[7] 65 65
else
J++;
}
(3)- Show the content of the arrays A and B as a result of executing the following
program:
After After
A B
#include <iostream>
using namespace std;
void main()
{
int A[7] = {66, 55, 77, 55, 88, 77, 66};
int B[7];
int num = 55;
int k = 0;
for (int j = 0; j < 6; j++)
if (A[j] > num)
{
B[k] = A[j];
A[j] = 0;
k++;
}
}
=================================================================================================================
(4)- Create a program segment that takes a 1-D array and flips all its variables. Example
below:
14 12
67 98
3 73
31 74
38 38
74 31
73 3
98 67
12 14
==========================================================
(5)- Write a C++ program that find the common elements between two
arrays of variable sizes. Your program should ask the user to input the sizes
of the two arrays and the elements in each of them. The program then
should display the common elements that appear in both arrays.
Sample Input:
4 7 11 3 9
8 1 4 12 10 7 2
Output:
4
7
================================================================
(6)- Write only the C++ code to create the following array and and fill it with the shown
data. Also, the the code to display the array content as a 2D array.
A X X X X
X A X X X
X X A X X
X X X A X
X X X X A
(7)- Write only the C++ code to create the following array and and fill it with the shown data. Also,
the the code to display the array content as a 2D array.
A X X X X
A A X X X
A A A X X
A A A A X
A A A A A
====================================================================
(8)- - Show the output of the following
void test ( int, int&, int&);
int main( )
{
int d = 5, e = 6, f = 7, a = 8, b = 9;
test (d, e, f);
cout<<“In the main program after the 1st. call”<<endl;
cout<< “the variables are: “ <<setw(3) << e <<setw(3)
<< d <<setw(4) << f <<endl;
cout<< “----------------------------------------------“ <<endl;
test (a, b, f);
cout<<“In the main program after the 2nd. call”<<endl;
cout<< “the variables are: “ <<setw(3) << a <<setw(3)
<<b<<setw(4) << f <<endl;
return 0;
}
void test(int t, int& s, int& x)
{
s = 10;
s = s +20;
x = x + s + t;
t =3 * s;
cout<< “Function test Output: “ <<setw(3) << s
<<setw(3) << t <<setw(3) << x <<endl;
}
------------------------------------------------------------------------------------------------------------
9-
Show the output of each of the following program segments:
a-
#include <iostream.h>
int main ()
{
int n=0;
for (int i=0; i<7; i++)
{
for (int j=0; j<=n; j++)
{
cout<<j;
}
n++;
cout<< endl
}
=============================================================
b-
int main ()
{
int n=1 , j=1;
for (int i=0; i<4; i++)
{
for (int k=j; k<=n*4; k++)
{
cout<<k;
}
n++;
j=j+4;
cout<< endl
}
}
=============================================================
c-
#include <iostream.h>
int main ()
{
int i=1;
while(i <= 128)
{
for (int k=0; k<4; k++)
{
cout<<i;
i=i*2;
cout<< endl
}
}
=====================================================
Arrays :
1. Write a C++ function that finds the second largest element in an array of
doubles.
=======================================================