printing numbers as a rectangle with nested for loops in c++ - c++

I am a beginner in programming.
I am currently writing a code that takes the number 1 to 9 of rows and columns from the user. for single digits, there should be a "0"
the output should look like this:
Type a row number between 1 and 9: 3
Type a column number between 1 and 9:7
01 02 03 04 05 06 07
08 09 10 11 12 13 14
15 16 17 18 19 20 21
This is the code i currently have:
#include <iostream>
int main() {
int r,c,i,j,n,k;
cout<<"Type a row number between 1 and 9: ";
cin>>r;
while (r<1 || r>9){
cout << "Please enter a number between 1 and 9.";
cin>>r;
}
cout<< "Type a column number between 1 and 9: ";
cin>>c;
while (c<1 || c>9){
cout << "Please enter a number between 1 and 9.";
cin>>c;
}
n=r*c;
for(k=0; k<n; k++){
}
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
cout<<i<<" ";
}
cout << endl;
}
return 0;
}
I've added validity statements for the users.
This outputs:
Type a row number between 1 and 9: 3
Type a column number between 1 and 9:7
0 0 0 0 0 0 0
1 1 1 1 1 1 1
2 2 2 2 2 2 2
I can't figure out the 0 and how to implement the numbers into the rectangle.
Please help.

You're close, just change your output loop to:
int counter = 1;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
cout << counter << " ";
++counter;
}
cout << endl;
}
If you would like to avoid the extra local variable you can also just do:
cout << (i*c + j + 1) << " ";
I would prefer the explicit version using counter as it makes it obvious what you're doing and you can easily switch the order of the loops/output if desired.

Related

Write a C++ program which will print (half pyramid) pattern of natural numbers

The task:
Write a C++ program which will print (half pyramid) pattern of natural numbers.
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
I have tried using this code, but its not giving the output:
#include <iostream>
using namespace std;
int main()
{
int rows, i, j;
cout << "Enter number of rows: ";
cin >> rows;
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= i; j++)
{
cout << j << " ";
}
cout << "\n";
}
return 0;
}
OUTPUT:
Enter number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
j in your inner loop is the 1 based index of the element in the current row (1,2,3 etc.).
Instead of printing it, you should print a counter that is increased over all the iterations.
Something like:
#include <iostream>
int main()
{
int rows, i, j;
std::cout << "Enter number of rows: ";
std::cin >> rows;
int n = 1;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
std::cout << n << " ";
n++; // for next iteration
}
std::cout << "\n";
}
return 0;
}
Output example:
Enter number of rows: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
A side note: better to avoid using namespace std - see here Why is "using namespace std;" considered bad practice?.

Align output in c++

int n, o = 2;
cout << "n="; cin >> n;
for(int i = 1; i <= n; i++) {
for(int j = n; j >= i; j--) {
cout << o << " ";
o += 2;
}
cout << endl;
}
My code outputs (when n is 4)
2 4 6 8
10 12 14
16 18
20
How would I align it to output this?
2 4 6 8
10 12 14
16 18
20
I am trying to align the output to the right in such a way that the numbers are aligned one below another (for example the 4 is aligned to the space between the 10 and 12 and I want to align it to 10). Thanks.
The header <iomanip> provides many input/output manipulators you can play with, one beeing std::setw, which sets the width parameter of the stream for the next item.
Changing the line in the posted snippet that outputs the numbers into
std::cout << std::setw(3) << o;
The output becames
2 4 6 8
10 12 14
16 18
20
Now, to produce the desired output, we can notice that the first number of each row can either be printed with an increasing "width" (3 the first row, 6 the second and so on) or after a loop that prints the right amount of spaces (0 the first row, 3 the second and so on).
just edited the inner loop of your code and this is the edited code:
#include <iostream>
using namespace std;
int main() {
int n, o = 2;
cout << "n="; cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j >= i) {
cout << o << "\t";
o += 2;
}
else
{
cout << "\t";
}
}
cout << endl;
}
}
and this is some example output:
n=4
2 4 6 8
10 12 14
16 18
20
what I did is to make the inner loop, loops from 1 till n like the outer loop and only check if the current column is greater than or equal to the current row, if so, then I print the number and increment it, if not then I just cout a space or a tab to be more specific

Small tweak to my c++ pyramid assignment? How should I do it?

I was tasked with this problem for homework for my c++ class and I can't figure it out.
The task is: Create a program that will create a pattern in which is a pyramid. The user should enter the maximum number of rows to be output. Use a while loop that confirms the number of rows is between 1 and 9 inclusive. Next 1 should be output in the first row, 222 output in the second row, 33333 should be output in the third row, etc. For example if the user entered 7 the following would be output.
The code I have now does this almost exactly, instead of outputting, for example 222 for the second row, it outputs 2 2
Here is what my code looks like:
#include <iostream>
using namespace std;
int main()
{
int rows, count = 0, count1 = 0, k = 0;
cout << "Please enter the number of rows." << endl;
cin >> rows;
while (rows > 9)
{
cout << "That is an invalid selection, please choose up to 9 rows." << endl;
cin >> rows;
}
for (int i = 1; i <= rows; ++i)
{
for (int space = 1; space <= rows - i; ++space)
{
cout << " ";
++count;
}
while (k != 2 * i - 1)
{
if (count <= rows - 1)
{
cout << i << " ";
++count;
}
k++;
}
count1 = count = k = 0;
cout << endl;
}
}
Any help is appreciated, I'm assuming it should just be a small tweak.
This loop
while (k != 2 * i - 1)
{
if (count <= rows - 1)
{
cout << i << " ";
++count;
}
k++;
}
count1 = count = k = 0;
does not make sense. For example the variable count1 is never used except the statement
count1 = count = k = 0;
So it is unclear what is the purpose to define this variable.
Using manipulators from the header <iomanip> you can write a pyramid using only one loop.
Here is a demonstrative program
#include <iostream>
#include <iomanip>
int main()
{
while ( true )
{
const int MAX_HEIGHT = 9;
std::cout << "Enter the height of a pyramid not greater than "
<< MAX_HEIGHT << " (0 - exit): ";
int height;
if ( not ( std::cin >> height ) or ( height <= 0 ) ) break;
if ( MAX_HEIGHT < height ) height = MAX_HEIGHT;
std::cout << '\n';
for ( int i = 0; i < height; i++ )
{
std::cout << std::setw( height - i ) << std::setfill( ' ' ) << i + 1;
std::cout << std::setw( 2 * i + 1 ) << std::setfill( char( i + '1' ) ) << '\n';
}
std::cout << '\n';
}
return 0;
}
Its output might look the following way
Enter the height of a pyramid not greater than 9 (0 - exit): 1
1
Enter the height of a pyramid not greater than 9 (0 - exit): 2
1
222
Enter the height of a pyramid not greater than 9 (0 - exit): 3
1
222
33333
Enter the height of a pyramid not greater than 9 (0 - exit): 4
1
222
33333
4444444
Enter the height of a pyramid not greater than 9 (0 - exit): 5
1
222
33333
4444444
555555555
Enter the height of a pyramid not greater than 9 (0 - exit): 6
1
222
33333
4444444
555555555
66666666666
Enter the height of a pyramid not greater than 9 (0 - exit): 7
1
222
33333
4444444
555555555
66666666666
7777777777777
Enter the height of a pyramid not greater than 9 (0 - exit): 8
1
222
33333
4444444
555555555
66666666666
7777777777777
888888888888888
Enter the height of a pyramid not greater than 9 (0 - exit): 9
1
222
33333
4444444
555555555
66666666666
7777777777777
888888888888888
99999999999999999
Enter the height of a pyramid not greater than 9 (0 - exit): 0

I keep getting zeros when after my original inputs

so im creating a vector with unlimted inputs but i want it so that when the user wants to stop inputing data, they just enter a non numerical key and then just move on to the next vector which is just the first one but reversed and multipled by 8 and 100 added to it but when it should move on, it just adds a bunch of zeros as inputs
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
vector<int> (myVec1)(n);
vector<int> (myVec2)(n);
int i;
int sum;
for(i=0;i<n;++i){
cout<<"please enter an integer ";
cin>>myVec1.at(i);
cout<<endl;
if(cin.fail()){
for(i=0; i<n; i++){
cout<<myVec1.at(i)<<" ";
}
}
}
cout<<endl;
for(i=0; i<n; i++){
sum=(myVec1.back()*8)+100;
myVec1.pop_back();
cout<<sum<<" ";
}
return 0;
}
this is the result and as you see theres just a bunch of zeros after i tried to stop the inputs if any one can help me id really appreciate it :)
please enter an integer 435
please enter an integer 412
please enter an integer 43
please enter an integer 56
please enter an integer 2
please enter an integer q
435 412 43 56 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 116 548 444 3396 3580
int n has undefined value (not initialized)
Instead, ignore defining n and initialize your vector like this: vector<int> myVec1;
and when you read values, append them in the vector using myVec1.push_back(valueFromKeyboard);
#include <iostream>
#include <vector>
using namespace std;
int main () {
vector<int> myVec1;
vector<int> myVec2;
int valueFromKeyboard;
cout << "please enter an integer ";
while (cin >> valueFromKeyboard) {
if (!cin.fail()) {
myVec1.push_back(valueFromKeyboard);
}
cout << "please enter an integer ";
}
for (int i = 0; i < myVec1.size(); i++) {
cout << myVec1[i] << " ";
}
cout << endl << endl;
for (int i = 1; i < myVec1.size()+1; i++) {
myVec2.push_back(myVec1.end()[-i]*8+100);
}
for (int i = 0; i < myVec2.size(); i++) {
cout << myVec2[i] << " ";
}
return 0;
}
myVec1.end()[-1] is the last element of the vector, myVec1.end()[-2] is the second to last etc. That's why I created this loop for (int i = 1; i < myVec1.size()+1; i++) to go from 1 to myVec1.size()+1

Loading different data types from text file

I'm new to StackExchange and C++, so apologies if I don't describe the problem well enough. I need some help with homework.
I'm trying to find a way to load this file and store its data. The file has different data types so what I came up with so far was to use struct array to store all the values, using a triple nested for loop to save the data in the correct variable.
Here is what the .dat file looks like, with "comments" to help describe what's going on.
100A 2 // model number, 2 different versions of that model
0 0 0 // number of quarters, dimes, and nickels
5 //number of items in the vending machine
1A 1034 5 // Code combonation, ID Number, Quantity
1B 1000 10
1C 1100 10
1D 1123 20
1E 1222 5
0 0 0 // number of quarter, dimes, nickels in 2nd model
7 // number of items in the second version of that model
1A 2180 20
1B 1283 20
1C 3629 5
1D 3649 3
1E 4051 15
1F 4211 1
1G 5318 5
100B 3 // New model, with 3 different versions of itself.
2 10 5 //everything repeats like model 100A
7
Here is the code I came up with
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct VMdata
{
ifstream inFile;
string model[1];
int version[1];
int q[5];
int d[5];
int n[5];
int size[5];
string id[30];
int code[30];
int num[30];
char dummy;
};
int main()
{
VMdata New;
cout << fixed << setprecision(2) << showpoint;
New.inFile.open("machines.dat");
cout << "Model Data"
<< endl << endl;
int count1 = 0;
int count2= 0;
int count3 = 0;
for (int i = 0; i < 2; i ++)
{
cout << "i :" << count1 << endl;
New.inFile >> New.model[i] >> New.version[i]; // loads model number and number of versions i times
count1 = count1 + 1;
for (int j = 0; j < New.version[count1 -1]; j ++)
{
cout <<"j :" << count2 << endl;
New.inFile >> New.q[count2] >> New.d[count2] >> New.n[count2] >> New.size[count2]; // loads number of q, d, n, j times
count2 = count2 + 1;
for (int k = 0; k < New.size[count2 - 1]; k++)
{
cout << "k :" << count3 << endl;
New.inFile >> New.id[count3] >> New.code[count3] >> New.num[count3]; // loads id number, code number, and total number k times
count3 = count3 + 1;
}
}
}
New.inFile.close();
count1 = 0;
count2= 0;
count3 = 0;
cout << endl;
for ( int i = 0; i < 2; i ++)
{
cout << New.model[i] << setw(12) << New.version[i] << endl << endl;
count1 = count1 + 1;
for (int j = 0; j < New.version[count1 -1]; j ++)
{
cout << New.q[count2] << setw(12) << New.d[count2] << setw(12) << New.n[count2] << endl << setw(12) << New.size[count2] << endl;
count2 = count2 + 1;
for (int k = 0; k < New.size[count2 - 1]; k++)
{
cout << New.id[count3] << setw(12) << New.code[count3] << setw(12) << New.num[count3] << endl << endl;
count3 = count3 + 1;
}
}
}
return 0;
}
Here is my test output.
100A 2
3 15 0 // should be 0, 0, 0
5
1A 1034 5
1B 1000 10
1C 1100 10
1D 1123 20
1E 1222 5
0 0 0
7
1A 2180 20
1B 1283 20
1C 3629 5
1D 3649 3
1E 4051 15
1F 4211 1
1G 5318 5
♥ ☻ 3 // the heart and smile should be 100B lol
2 10 5
7
1A 2180 10
1B 1283 10
1C 3629 5
1D 3649 3
1E 4051 15
1F 4211 10
1G 3026 5
5 6 3
6
1A 6626 5
1B 6155 5
1C 5982 10
1D 5573 3
1E 5454 10
1F 5336 50
10 10 10
5
1A 1034 5
1B 1000 5
1C 1100 5
1D 1123 5
1E 1210 12
Press any key to continue . . .
As you can see, the number of quarters, dimes, and nickels is wrong for the first machine, and the model name is wrong for the 2nd model.
If anyone has any suggestions it would be much appreciated.
It looks like you are clobbering data. Your struct allocates only one model and one version, but the i index takes values of 0 and 1. The second time these are meant to be read in, the address is overwriting your coin counts.