What do I have to post on c in order to get an output Im com
What do I have to post on c++ in order to get an output I\'m completely lost
To learn how nested for loops work, do a walk-through of the following program segments and determine, in each case, the exact output.
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= 5; j++)
cout << setw(3) << i;
cout << endl;
}
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = (i + 1); j <= 5; j++)
cout << setw(5) << j;
cout << endl;
}
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
cout << setw(3) << j;
cout << endl;
}
const int M = 10;
const int N = 10;
int i, j;
for (i = 1; i <= M; i++)
{
for (j = 1; j <= N; j++)
cout << setw(3) << M * (i - 1) + j;
cout << endl;
}
int i, j;
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= (9 - i); j++)
cout << \" \";
for (j = 1; j <= i; j++)
cout << setw(1) << j;
for (j = (i - 1); j >= 1; j--)
cout << setw(1) << j;
cout << endl;
}
Solution
Here is the code explained as comments for you:
#include <iostream>
using namespace std;
int countLetters(string str, char c)
{
int i;
int x = 0;
for(i = 0; i < (int)str.length(); i++)
if(str[i] == c)
x = x + 1;
return x;
}
int main()
{
int i, j;
for (i = 1; i <= 5; i++) //This loop runs for values 1, 2, 3, 4, and 5.
{
for (j = 1; j <= 5; j++) //This loop runs for values 1, 2, 3, 4, and 5.
cout << setw(3) << i; //For each j value, the i value will be printed.
cout << endl; //And moved to the next line.
}
//So, the output is:
//1 1 1 1 1
//2 2 2 2 2
//3 3 3 3 3
//4 4 4 4 4
//5 5 5 5 5
int i, j;
for (i = 1; i <= 5; i++) //This loop runs for values 1, 2, 3, 4, and 5.
{
for (j = (i + 1); j <= 5; j++) //This loop runs for values starting from i+1 to 5.
cout << setw(5) << j; //For each j value, j is printed.
cout << endl; //And moved to the next line.
}
//So, the output is:
//2 3 4 5
//3 4 5
//4 5
//5
int i, j;
for (i = 1; i <= 5; i++) //This loop runs for values 1, 2, 3, 4, and 5.
{
for (j = 1; j <= i; j++) //This loop runs for values from 1 to i.
cout << setw(3) << j; //For each j value, j is printed.
cout << endl; //And moved to the next line.
}
//So, the output is:
//1
//1 2
//1 2 3
//1 2 3 4
//1 2 3 4 5
const int M = 10;
const int N = 10;
int i, j;
for (i = 1; i <= M; i++) //This loop runs for values 1 to M(i.e., 10)
{
for (j = 1; j <= N; j++) //This loop runs for values 1 to N(i.e., 10)
cout << setw(3) << M * (i - 1) + j; //For each j value, 10 * (i-1) + j is printed.
cout << endl; //And moved to the next line.
}
//So, the output is:
//1 2 3 4 5 6 7 8 9 10
//11 12 13 14 15 ...
//.
//.
//.
//91 92 93 ... 100
//
int i, j;
for (i = 1; i <= 9; i++) //This loop runs for values 1 to 9.
{
for (j = 1; j <= (9 - i); j++) //This loop runs for values 1 to 9-i
cout << \" \"; //Spaces will be printed.
for (j = 1; j <= i; j++) //This loop runs for values 1 to i.
cout << setw(1) << j; //j value is printed.
for (j = (i - 1); j >= 1; j--) //This loop runs for values i-1 down to 1.
cout << setw(1) << j; //j value is printed.
cout << endl; //And moved to the next line.
}
}
//So, the output is:
//. . . . . . . . 1
//. . . . . . . 1 2 1
//. . . . . . 1 2 3 2 1
//...


