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;
}
}
?
Related
Write a program that reads 12 integers into a 2D integer array with 4 rows and 3 columns. The program then outputs the 2D array in reverse order according to both rows and columns.
Ex: If the input is:
5 7 3
6 4 3
5 6 9
5 2 8
then the output is:
8 2 5
9 6 5
3 4 6
3 7 5
For coding simplicity, output a space after every integer, including the last one on each row.
#include <iostream>
using namespace std;
int main() {
const int ROWS = 4;
const int COLS = 3;
int arr[ROWS][COLS];
int i, j;
for(i = 0; i < ROWS; i++){
for(j = 0; j < COLS; j++){
cin>>arr[i][j];
}
}
cout << arr[3][2] << " " << arr[3][1] << " " << arr[3][0] << " " << endl;
cout << arr[2][2] << " " << arr[2][1] << " " << arr[2][0] << " "<< endl;
cout << arr[1][2] << " " << arr[1][1] << " " << arr[1][0] << " "<< endl;
cout << arr[0][2] << " " << arr[0][1] << " " << arr[0][0] << " "<< endl;
return 0;
}
I ended up having to hardcode this question because I couldnt find a way to reverse the 2D array with a loop and get it to be outputted in the form of a graph. Is there a way i could reverse the 2D array using for loops and would it be possible to be able to change the amount of rows and columns and still output the corresponding graph of values?
try this:
#include <iostream>
using namespace std;
int main() {
const int ROWS = 4;
const int COLS = 3;
int arr[ROWS][COLS];
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
cin >> arr[i][j];
}
}
// output the reversed array
for (int i = ROWS - 1; i >= 0; i--) {
for (int j = COLS - 1; j >= 0; j--) {
cout << arr[i][j] << " ";
}
cout << endl;
}
return 0;
}
You can reverse a 2D array using nested for loops, try
#include <iostream>
using namespace std;
int main() {
const int ROWS = 4;
const int COLS = 3;
int arr[ROWS][COLS];
int i, j;
// Input the values into the 2D array
for(i = 0; i < ROWS; i++) {
for(j = 0; j < COLS; j++) {
cin >> arr[i][j];
}
}
// Reverse the rows and columns of the 2D array
for(i = ROWS - 1; i >= 0; i--) {
for(j = COLS - 1; j >= 0; j--) {
cout << arr[i][j] << " ";
}
cout << endl;
}
return 0;
}
As mentioned in comments below if you don't know ROWS and COLS size at compile time dynamically allocate the memory for 2D array(arr) in C++ using new operator.
There is very little point reading the data into a 2D array for this program. A std::vector would do the trick, sized with ROWS * COLS values. You then have the benefit of being able to read those dimensions from the user, which addresses the second part of your question.
size_t size = ROWS * COLS;
// Read data
std::vector<int> data;
data.reserve(size);
for (int value; std::cin >> value; )
{
data.push_back(value);
}
// Validate data
if (data.size() != size)
{
std::cerr << "Unexpected end of input!\n";
return EXIT_FAILURE;
}
When outputting, you can use a reverse iterator through the vector, and simply write a newline every COLS values.
// Output in reverse
int col = 0;
for (auto it = data.rbegin(); it != data.rend(); it++)
{
std::cout << *it << " ";
if (++col == COLS)
{
std::cout << "\n";
col = 0;
}
}
You can even easily fix the "space at the end of the line" problem by adjusting your output loop as follows:
// Output in reverse
int col = 0;
for (auto it = data.rbegin(); it != data.rend(); it++)
{
std::cout << *it;
if (++col == COLS)
{
std::cout << "\n";
col = 0;
}
else
{
std::cout << " ";
}
}
I have an array of integer values that is of one dimension.I need to print it as columns.
Although I can print the values as columns but it pritns more than 5 values. The ideal output would be like this:
1 1 1
1 1
How would i do this?
For example, my try is this:
int main(){
int array[5] = {1,1,1,1,1};
cout<<"array printed in the form of columns: "<<endl;
for ( int i = 0; i < 5; i++ ){
cout<<setw(3)<< array[i]<<endl<<setw(3)<<array[i];
}
return 0;
}
If as columns, then it the
for (int i=0; i<5; i++) {
cout << a[i] << endl;
}
would be sufficient.
But you probably want the row-representation. (1 1 1 1 1, right?). Then
for (int i=0; i<5; i++) {
cout << a[i] << " ";
}
would suffice. Or, you can use a pointer
int * p_array = array;
and then, inside the loop
cout << *(p_array + i) << " ";
EDIT:
That was for your unedited question :-) For the new output -> you can follow the answer of the Cigien
I guess you could simply insert an end line after each 3(or any number of columns that you want) element printed.
Here is an exemple with a longer array :
int main()
{
int colNum = 3;
int array[7] = {1,1,1,1,1,1,1};
int len = (sizeof(array)/sizeof(array[0]));
cout<<"array printed in the form of columns: " << endl;
for ( int i = 0; i < len ; i++ )
{
cout << array[i];
if ((i+1)%colNum==0) cout << '\n';
}
return 0;
}
You can just write 2 loops:
for ( int i = 0; i < 3; ++i ) {
cout << setw(3) << array[i]; // first row
}
cout << endl; // next row
for ( int i = 3; i < 5; ++i ) {
cout << setw(3) << array[i]; // second row
}
I'm having issues with this c++ code. It is supposed to print a hollow right isosceles triangle, but instead just prints asterisks over and over, so the for loops seem to be stuck.
#include "pch.h"
#include <string>
#include <iostream>
int main() {
int row;
std::string s = " ";
std::string a = " *";
int rows = 10;
for (int i = 0; i < rows; i++) {
if (i = 0) {
std::cout << a << std::endl;
}
while (i > 2 && i < rows) {
std::cout << a;
for (int pos = 0; pos < i; pos++) {
std::cout << s;
}
std::cout << a << std::endl;
}
std::cout << a << a << a << a << a << a << a << a << a << std::endl;
}
}
your while loop condition will never become false, AND you need to use comparison (==) instead of assignment in this line:
if (i = 0) {
Supposing that what you want to print is something of the following form:
Eg. for rows = 5
*
**
* *
* *
*****
Your code should have the following structure:
for (int i = 1; i <= rows; ++i)
{
//special case for the first line
if (i == 1)
std::cout << asterisk << std::endl;
//for each of the other lines print 2 asterisks and the rest spaces
if (i > 1 && i <= rows - 1)
{
//one at the start of the line
std::cout << asterisk;
//print line - 2 spaces
for (int j = 0; j < i - 2; ++j)
std::cout << space;
//one at the end of the line
std::cout << asterisk << std::endl;
}
//special case for the last line
if (i == rows)
{
for (int j = 1; j <= i; ++j )
std::cout << asterisk;
std::cout << endl;
}
}
https://ideone.com/peGRUG
Your while loop condition is the issue here, also you should use == instead of = inside if condition. Anyways,Here is a small fix in your solution..
void printTriangle() {
int row;
std::string s = " ";
std::string a = " *";
int rows = 10;
for (int i = 1; i < rows-1; i++) {
for (int j = 1; j <= i; ++j)
{
if (j == 1 || j == i)
std::cout << a;
else
std::cout << s;
}
std::cout << std::endl;
}
for (int i = 1; i < rows; ++i)
std::cout << a;
}
Still new to this. What is wrong with this code? I'm trying to make and use a 2 dimensional array. Is my general idea correct? To step through it with nested for loops? What exactly is wrong with my code? It won't compile.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const double NUM_MONKEYS = 3;
const double NUM_DAYS = 5;
double monkeys[NUM_MONKEYS][NUM_DAYS];
int row, column;
for (row = 0, row < NUM_MONKEYS, row++)
{
for (column = 0, column < NUM_DAYS, column++)
{
cout << "Input amount of food eaten by monkey: " << row + 1;
cout << " and day: " << column + 1 << endl;
cin >> monkeys[row][column];
}
}
return 0;
}
There's something I'm not getting, thanks!
First of all - size of array should be of integer type and you have defined it as double.
Second - Syntax of for loop is incorrect, there should be ';' instead of ',' in your for loop.
#include <iostream>
#include <iomanip>
int main()
{
const int NUM_MONKEYS = 3;
const int NUM_DAYS = 5;
double monkeys[NUM_MONKEYS][NUM_DAYS];
int row, column;
for (row = 0; row < NUM_MONKEYS; row++)
{
for (column = 0; column < NUM_DAYS; column++)
{
std::cout << "Input amount of food eaten by monkey: " << row + 1;
std::cout << " and day: " << column + 1 << endl;
std::cin >> monkeys[row][column];
}
}
return 0;
}
Though you can store double type values in your array.
Also as said try and avoid 'using namespace std;' see here.
The purpose of this code is to add the elements in the two arrays, but in reverse order. I don't understand what I did wrong for this not to Compile(Syntax,Loop, or Array Error??). Could you point me in the right direction? Thank you!!
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
const int ARRAY1_LEN = 3;
const int ARRAY2_LEN = 2;
int MyInts1[ARRAY1_LEN] = { 35, -3, 0};
int MyInts2[ARRAY2_LEN] = {20, -1};
cout << "Multiplying each int in MyInt1 by each in MyInts2 ... But Backwards:" << endl;
for(int Array1Index = 0; Array1Index < ARRAY1_LEN - 1; Array1Index--);
for(int Array2Index = 0; Array2Index < ARRAY2_LEN -1; Array2Index--);
cout << MyInts1[Array1Index] << " x " << MyInts2[ Array2Index ] << " = " << MyInts1[Array1Index] * MyInts2[Array2Index] << endl;
return 0;
}
Your logic is not correct. You are starting at index 0 and then going backward. It means, that you are entering negative range (-1, -2, -3, ...) and every negative number satisfies loop condition.
It rather should be:
int main()
{
const int ARRAY1_LEN = 3;
const int ARRAY2_LEN = 2;
int MyInts1[ARRAY1_LEN] = { 35, -3, 0 };
int MyInts2[ARRAY2_LEN] = { 20, -1 };
cout << "Multiplying each int in MyInt1 by each in MyInts2 ... But Backwards:" << endl;
int start_1 = ARRAY1_LEN > 0 ? ARRAY1_LEN - 1 : 0;
int start_2 = ARRAY2_LEN > 0 ? ARRAY2_LEN - 1 : 0;
for(int Array1Index = start_1; Array1Index >= 0; Array1Index--)
{
for(int Array2Index = start_2; Array2Index >= 0; Array2Index--)
{
cout << MyInts1[Array1Index] << " x " << MyInts2[ Array2Index ] << " = " << MyInts1[Array1Index] * MyInts2[Array2Index] << endl;
}
}
return 0;
}
This code will also work properly if at least one of the arrays will be empty.
Oh, and one more thing: your code was also totally wrong, because you had semicolons (;) after for, which means, that every loop had an empty body. So, even if your fors were correct, you wouldn't see anything anyway.
There are two errors in your program:
The whole loop body consits only of the semicolons after the for loop instead of the code you actually want to run inside the loops. This also explains, why your code doesn't compile: your multiplication is not part of the loop bodies and as a result Array1Index and Array2Index - which are defined in the loop headers - do no longer exist.
Although you are decreasing the arry indices, you still start at 0, so you would access negative array indices.
So your code should actually look like this:
for (int Array1Index = ARRAY1_LEN - 1 ; Array1Index >= 0; Array1Index--){
for (int Array2Index = ARRAY2_LEN - 1; Array2Index >=0; Array2Index--){
cout << MyInts1[Array1Index] << " x " << MyInts2[Array2Index] << " = " << MyInts1[Array1Index] * MyInts2[Array2Index] << endl;
}
}