Need 3 separate outputs - c++

#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.

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
[?] [?] [?]

How to set up a partition without sorting?

I am having trouble creating a partition for set inputs. I have tried two things, the first is commented out and it is just some basic pseudo code for a partition algorithm and the second is what I interpreted from the class notes. First off the main function is fine, that was given to us. I only need to finish coding the partition function. Everything commented in the partition function is my first attempt, everything not commented is my second attempt.
Here is some inputs we are supposed to test along with the proper results:
1 2 3 4 5 6 7  pivot is 4; elements less than pivot are in the left section
8 7 6 5 4 3 2 1  pivot is 5; elements less than pivot are in the left section
5 1 4 3 6 7 8 2  pivot is 3; elements less than pivot are in the left section
4 3 1 2 7 5  pivot is 1; the left section is empty
2 6 8 4 1 7  pivot is 8; the right section is empty
int partition(int a[], int first, int last) {
/* int pivot = a[last / 2];
//cout << pivot << endl;
int i = (first - 1);
for (int j = first; j <= last; j++) {
if (a[j] < pivot) {
i++;
//cout << pivot << endl << i << endl << j << endl;
swap(a[i], a[j]);
}
}
cout << pivot << endl << i << endl << first << endl << last << endl;
//swap(a[i + 1], a[last]);
return (i + 1);
}*/
swap(a[first],a[(first+last)/2]);
int pivot = a[first];
int i = a[first];
int j = a[last];
while (i <= j)
{
if (pivot > a[i])
{i++;}
if (pivot < a[j])
{j--;}
if (i <= j)
{swap(a[i],a[j]);}
}
swap(pivot, a[j]);
return i;
}
int main()
{
int x; // number of elements
int nums[10];
cout << "How many elements would you like to enter into the array? (must
be less than 10): ";
cin >> x;
cout << "Enter the elements one per line" << endl;
for (int i = 0; i < x; i++)
{ cin >> nums[i]; }
int pivot_index = partition(nums, 0, x-1);
cout << "The array was partitioned" << endl;
// display up to the pivot without endl
for (int i = 0; i < pivot_index; i++)
cout << nums[i] << " ";
// display the pivot
cout << "| " << nums[pivot_index] << " | " ;
// display from the pivot without endl
for (int i = pivot_index+1; i < x; i++)
cout << nums[i] << " ";
cout << endl;
}

Trying to certain output with nested for loops

#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;
}
}

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.

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;
}
}
?