Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
For-while exercises
Create a flowchart and a C++ program fragment for
1. prints the product of the first 7 numbers
2.reads in n and prints the sum of the first n numbers- 1+2+3+...+ n-1+n
3. reads in n and determines the product of the first n numbers- 1*2*....*n1*n
4. reads in floating point numbers representing coins and prints the total
until the total is over 1.00
5. reads in floating point numbers representing coins and prints the total
until the total is over 1.00 and prints how many numbers have been read in
6. Determines the maximum of 20 numbers that are read in
7. Reads in n and determines the average of n grades that are read in.
8. prints a table of cos(x) for x=0, .1,.2,,,1.0
9 Reads in 20 numbers and counts the number of positive numbers.
10 The fibonacci sequence are the numbers 1 1 2 3 5 8, 13 ... so that a
number is the sum of the previous 2 numbers. Write a program that prints
the sequence of fibonacci numbers less than 100.
11 Write a program that for h= .1, .01, .001,...until 1.e-12 computes
df=(sin(1+h)-sin(1))/h and prints df and df-cos(1).
12. Prints 2 by 3 rectangle
x xx
x xx
13. Prints a 3 x 3 triangle
x
xx
xxx
14 Read n and prints an n x n triangle as in 13.
15 Prints a 3 x 3 triangle like
xxx
xx
x
16 reads in n and prints an n x n triangle as in 15.
17. Reads in x and n and computes x to the nth power without the pow
function.
Bwhat does these codes print?
a. int i=10;
while( i>0)
{
cout <<i<<” “;
i--;
}
b. int sum=0; j=1;
while(j<7)
{
sum=sum+j;
cout <<j<<” “ << sum<<endl;
}
c. int pow2=1;
while(pow2<500)
{
cout <<pow2<<” “;
pow2=pow2*2;
}
d. int num=528;
while (num>0)
{
cout <<num%10;
num=num/10;
}
e. double x=5, pow=1;
int n=3;
while(n>0)
{
pow=pow*x;
n--;}
cout <<x<< “ to the “<<n << “ is “<<pow;
f. int last=1,nexttolast=1,new;
while(last <15)
{
new=last+nexttolast;
cout <<new<<” “;
nexttolast=last;
last=new;
}
g. int j=1, k;
while(j<4)
{
k=1;
while(k<3)
{cout <<j <<” “<< k <<endl;
k++;
}
j++;
}
h.
for ( int j=1;j<=5;j++)
cout<<j<<” “;
i.
int sum=0;
for(j=1;j<5:j++)
{ sum=sum+j:
cout<<j<<” “<<sum<<endl;}
j. assuming the grades read in are 70 90 80 95;
int grade, max, n=4;
cin>>grade;
max= grade;
for (int j= 2;j<=4;j++)
{
cin>>grade;
if (grade>max)
{max=grade;
cout<<”max is “<<max<<endl;
}
}
k. for (int j=0; j<5; j++)
{
for ( int k =0;k<5;k++)
cout<<j+k<<” “;
cout<<endl;
}
l. for (int j=1; j<3; j++)
{
for ( int k =1;k<4;k++)
cout<<”*”;
cout<<endl;
}
m. for (int j=1; j<5; j++)
{
for ( int k =1;k<j;k++)
cout<<*<<” “;
cout<<endl;
}
3. what’s wrong?
A.int i=1;
while (i>0)
cout << i <<endl;
B. int i=1:
while (i<10)
i--;