Trying to certain output with nested for loops - c++

#include <iostream>
using namespace std;
int main (){
int row,
column = 0,
colCount = 3,
rowCount = 3;
for (column; column < colCount; column++){
for(row = 0; row <= (rowCount - 1); row++){
cout << column << " " << row;
if(row < (rowCount - 1)){
cout << ", ";
}
}
cout << column;
cout << endl;
}
}
Currently producing this output:
0 0, 0 1, 0 20
1 0, 1 1, 1 21
2 0, 2 1, 2 22
Trying to produce this output:
0 0, 0 1, 0 2
1 0, 1 1, 1 2
2 0, 2 1, 2 2

Remove
cout << column;
right above
cout << endl;
Final code:
#include <iostream>
using namespace std;
int main (){
int row,
column = 0,
colCount = 3,
rowCount = 3;
for (column; column < colCount; column++){
for(row = 0; row <= (rowCount - 1); row++){
cout << column << " " << row;
if(row < (rowCount - 1)){
cout << ", ";
}
}
cout << endl;
}
}

Related

Sum of rows and column in a Matrix

the problem arises when I use a different number of rows and columns, for example, 2 by 3 otherwise, it is running okay. the sum of column outputs garbage values
I can't seem to understand where the bug is.
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
int a[10][10];
int i,row,column, j, s = 0, sum = 0;
cout<<"Enter Number of rows: ";
cin>>row;
cout<<"Enter Number of columns: ";
cin>>column;
cout<< "Enter elements Matrix \n";
for (i = 0; i < row; i++)
for (j = 0; j < column; j++)
cin >> a[i][j];
cout << "Matrix Entered By you is \n";
for (i = 0; i < row; i++)
{
for (j = 0; j <column; j++)
cout << a[i][j] << " ";
cout << endl;
}
for (i = 0; i < row; i++)
{
for (j = 0; j <column; j++)
s = s + a[i][j];
cout << "Sum of Row " << i + 1 << " is: " << s;
s = 0;
cout << endl;
}
cout << endl;
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
s = s + a[j][i];
cout << "Sum of Column " << i + 1 << " is: " << s;
s = 0;
cout << endl;
}
}
You are not iterating correctly to get your columns sum, column and row are switched up. change to:
for (i = 0; i < column; i++) // <-----
{
for (j = 0; j < row; j++) // <-----
s = s + a[j][i];
cout << "Sum of Column " << i + 1 << " is: " << s;
s = 0;
cout << endl;
}
Consider a 3x4 matrix:
1 2 3 4
1 2 3 4
1 2 3 4
Your current loop would access it in the following manner, invoking undefined behavior.
[1] [2] [3] 4
[1] [2] [3] 4
[1] [2] [3] 4
[?] [?] [?]

Need 3 separate outputs

#include <iostream>
using namespace std;
int main (){
int row,
column = 0,
colCount = 3,
rowCount = 3;
//for loop
for (column; column < colCount; column++){
for(row = 0; row <= (rowCount - 1); row++){
cout << column << " " << row;
if(row < (rowCount - 1)){
cout << ", ";
}
}
cout << endl;
}
//while loop
while(column < colCount){
while(row < rowCount){
cout << column << " "<< row;
if(row < (rowCount - 1)){
cout << ", ";
}
row++;
}
cout << endl;
column += 1;
row = 0;
}
//do while loop
do{
do{
cout << column << " "<< row;
if(row < (rowCount - 1)){
cout << ", ";
}
row++;
}while(row < rowCount);
cout << endl;
column +=1;
row = 0;
}while(column < colCount);
}
When commenting out 2/3 loops, each one will produce the wanted output.
All together it seems to run on top of each other and adds extra output.
Current output:
0 0, 0 1, 0 2
1 0, 1 1, 1 2
2 0, 2 1, 2 2
3 3
Wanted output:
0 0, 0 1, 0 2
1 0, 1 1, 1 2
2 0, 2 1, 2 2
0 0, 0 1, 0 2
1 0, 1 1, 1 2
2 0, 2 1, 2 2
0 0, 0 1, 0 2
1 0, 1 1, 1 2
2 0, 2 1, 2 2
How do I get an output out of each loop?
You can leave the for loop as it is:
for (; column < colCount; column++){
for(row = 0; row <= (rowCount - 1); row++){
std::cout << column << " " << row;
if(row < (rowCount - 1))
std::cout << ", ";
}
std::cout << std::endl;
}
Now column and row gets to 3 in the for-loop above, which makes it the while loop never going to be executed. So, you need to make both of them to 0.
And, the third do-while loop always executes before any condition checks, thats why you get 3 3
Anyways, here is a solution for your problem.
#include <iostream>
int main ()
{
int row, column = 0, colCount = 3, rowCount = 3;
//for loop
for (column; column < colCount; column++){
for(row = 0; row <= (rowCount - 1); row++){
std::cout << column << " " << row;
if(row < (rowCount - 1))
std::cout << ", ";
}
std::cout << std::endl;
}
std::cout<<std::endl;
//while loop
column = 0;
row = 0;
while(column < rowCount){
while(row < rowCount){
std::cout << column << " "<< row;
if(row < (rowCount - 1))
std::cout << ", ";
row++;
}
std::cout << std::endl;
column += 1;
row = 0;
}
//do while loop
std::cout<<std::endl;
column = 0;
row = 0;
do{
do{
std::cout << column << " "<< row;
if(row < (rowCount - 1))
std::cout << ", ";
row++;
}while(row < rowCount);
std::cout << std::endl;
column +=1;
row = 0;
}while(column < colCount);
return 0;
}
1) You should initialize the column at the beginning of each loop to 0.
2) Within the while and do loops, initialize the row to 0 before entering the inner-loop
The output 3 3 at the end is due to a single entrance in the do-while loop.

Enter values into a 2d array and print

I am trying to write a C++ program that asks for inputs for a 3 by 3 array and then prints them. I am trying to print it but am not sure why it is just giving me 0 for each val
#include <iostream>
using namespace std;
const int row = 3;
const int col = 3;
void printMatrix(int array[row][col])
{
int i, j;
cout << endl << "Matrix " << endl;
for(i = 0; i < row; i++)
{
cout << endl;
for(j = 0; j < col; j++)
{
cout << array[row][col] << "\t";
}
}
cout << "\n";
}
int main()
{
int i, j, array[row][col];
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
cout << "Enter a value for Row " << i + 1 << " Col " << j + 1 << ": "
cin >> array[i][j];
}
}
printMatrix(array);
}
Console Output:
Enter a value for Row 1 Col 1: 1
Enter a value for Row 1 Col 2: 2
Enter a value for Row 1 Col 3: 3
Enter a value for Row 2 Col 1: 1
Enter a value for Row 2 Col 2: 2
Enter a value for Row 2 Col 3: 3
Enter a value for Row 3 Col 1: 1
Enter a value for Row 3 Col 2: 2
Enter a value for Row 3 Col 3: 3
Output Matrix:
0 0 0
0 0 0
0 0 0
the problem is here
cout << array[row][col] << "\t";
try
cout << array[i][j] << "\t";
You just need to change the printMatrix() function.
Change this function as below:
cout << array[i][j] << "\t";
And put semicolon (;) at the end of the cout statement in the main function.

Wrong output while printing a multidimensional array

i have some trouble while printing this pseudo-multidimensional array , with elements that are set already.
And the point of it is to swap the first and third row and 2nd and 4th column, but the output looks weird...
#include <iostream>
using namespace std;
int main()
{
int arr[12]= {
1,2,3,4,5,6,7,8,6,4,5,3
};
cout << "Before change: "<<endl;
for (int row=0;row<3;row++){
for (int col=0;col<4;col++){
cout << arr[row*col]<<" ";
}
cout <<endl;
}
cout << "After the row change: "<<endl;
for (int row=2;row>=0;row--){
for(int col=0;col<4;col++){
cout<<arr[row*col]<<" ";
}
cout<<endl;
}
cout << "After the column change: "<<endl;
int temp;
for(int row=0;row<3;row++){
temp=arr[row*1];
arr[row*1]=arr[row*3];
arr[row*3]=temp;
for (int col=0;col<4;col++){
cout<<arr[row*col]<<" ";
}
cout<<endl;
}
Instead of having an output like this:
1 2 3 4
5 6 7 8
6 4 5 3
6 4 5 3
5 6 7 8
1 2 3 4
6 3 5 4
5 8 7 6
1 4 3 2
i get this :
1 1 1 1
1 2 3 4
1 3 5 7
1 3 5 7
1 2 3 4
1 1 1 1
1 1 1 1
1 4 3 2
1 7 5 3
You wrong how to calculate the element inside the array arr[row*col] will be always 0 for the first row ( row = 0). So you have to do something like this:
#include <iostream>
using namespace std;
int main()
{
int arr[12] = {
1, 2, 3, 4, 5, 6, 7, 8, 6, 4, 5, 3
};
**int dimCol = 4;**
cout << "Before change: " << endl;
for (int row = 0; row < 3; row++){
for (int col = 0; col < 4; col++){
cout << arr[**(row*dimCol) + col**] << " ";
}
cout << endl;
}
cout << "After the row change: " << endl;
for (int row = 2; row >= 0; row--){
for (int col = 0; col < 4; col++){
cout << arr[**(row*dimCol) + col**] << " ";
}
cout << endl;
}
cout << "After the column change: " << endl;
int temp;
for (int row = 0; row < 3; row++){
temp = arr[row * 1];
arr[row * 1] = arr[row * 3];
arr[row * 3] = temp;
for (int col = 0; col < 4; col++){
cout << arr[**(row*dimCol) + col**] << " ";
}
cout << endl;
}
}
The formule will be: array[row*numberOfColumn + Column]
You got a wrong argument definition:
Instead of
array[row*col]
write this
array[row*4 + col]
So the formula is:
array[row*total_col + col]
The loop you are using multiplies each time your variable with Zero that's why you are getting 1 at the beginning of every line as your first element is 1, and arr[0] is 1.
*and your line 1 is
1 1 1 1*
because value of outer loop is zero and any value of variable of inner loop multiplied will result in 0.
the reason you are not getting correct output is your logic to print all element is not correct.
go for
array[row*4 + col]

How to access elements in an array in reverse order?

So I don't understand what I am doing wrong(Syntax error, Nested loop, or just silly mistake??). My compiler ask me to Press any key to continue . . . Could you point me in the right direction??
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
const int MAX_ROWS = 2;
const int MAX_COLS = 4;
int BigSmall[MAX_ROWS][MAX_COLS] =
{
{1,3,5,7},
{2,4,6,8}
};
for( int Row = 2; Row > MAX_ROWS; Row--)
{
for( int Column = 4; Column > MAX_COLS; Column--)
{
cout << "Integer[" << Row << "][" << Column << "] = " << BigSmall[Row][Column] << endl;
}
}
return 0;
}
for( int Row = 2; Row > MAX_ROWS; Row--)
Row and MAX_ROWS both equal 2 what you want is Row > 0
Same goes for the column loop
Syntax error, Nested loop, or just silly mistake?
The later: you start your inner loop at four, and continue while Column is above four. This is the same as never starting it. The outer loop has the same issue.
You should start your loop at SIZE-1 (i.e. MAX_ROWS - 1 or MAX_COLS - 1) and continue while you are above or at zero:
for( int Row = MAX_ROWS-1; Row >= 0 ; Row--) {
for( int Column = MAX_COLS - 1; Column >= 0 ; Column--) {
cout << "Integer[" << Row << "][" << Column << "] = " << BigSmall[Row][Column] << endl;
}
}
Demo.
Do you mean
for( int Row = MAX_ROWS - 1; Row >=0 ; Row--)
{
for( int Column = MAX_COLS - 1; Column >= 0 ; Column--)
{
cout << "Integer[" << Row << "][" << Column << "] = " << BigSmall[Row][Column] << endl;
}
}
?