How come no matter what I do for rows and columns my columns won't go above two? It should be 8x8 adding the 8 numbers together 8 times. I don't know what I'm doing wrong. Thank you
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main() {
srand(time(0));
int array1[8][8];
int array2[8][8];
int addition[8][8];
for (int i = 0; i < 7; i ) {
for (int j = 0; j < 7; j )
array1[i][j] = rand() % 6;
}
for (int i = 0;i < 7; i ) {
for (int j = 0; j < 7; j ) {
array2[i][j] = rand() % 8;
}
}
{
for (int i = 0; i < 7; i ) {
for (int j = 0; j < 7; j ) {
addition[i][j] = array1[i][j] array2[i][j];
}
for (int i = 0; i < 7; i ) {
for (int j = 0; j < 7; j ) {
cout << array1[i][j];
cout << " " << array2[i][j];
cout << " " << endl;
cout << "both previous numbers added together = " << addition[i][j] << endl;
}
}
return 0;
}
}
}
CodePudding user response:
Hi look carefully at the code structure. you had some extra brackets. This is the correct structure:
#include <iostream>
//using namespace std; - use this only in simple projects (my opinion).
int main()
{
srand(time(0));
int array1[8][8];
int array2 [8][8];
int addition[8][8];
for (int i = 0;i < 8;i )
{
for (int j = 0; j< 8;j )
array1[i][j] = rand() % 6;
}
for (int i = 0;i < 8;i )
{
for (int j = 0;j < 8;j )
array2[i][j] = rand() % 8;
}
//{ - you dont need this here
for (int i = 0;i < 8;i )
{
for (int j = 0;j < 8;j )
addition[i][j] = array1[i][j] array2[i][j];
}
for (int i = 0;i < 8;i )
{
for (int j = 0;j < 8;j )
{
std::cout << array1[i][j];
std::cout << " " << array2[i][j];
std::cout << " " << std::endl;
std::cout << "both previous numbers added together = " << addition[i][j] << std::endl;
}
}
//} - and you don't need this here
return 0;
}
Take this example and compare to your code to see your mistake. Code just wasn't structured properly.
CodePudding user response:
Your code's logics are absolutely right! However, the mistake was found in improper bracket structuring on for loop. I have corrected your code and mentioned the mistakes as comments.
#include <iomanip>
#include <cstdlib>
#include <iostream> //include this header to use "cout"
using namespace std;
int main() {
srand(time(0));
int array1[8][8];
int array2[8][8];
int addition[8][8];
for (int i = 0; i < 7; i ) {
for (int j = 0; j < 7; j )
array1[i][j] = rand() % 6;
}
for (int i = 0;i < 7; i ) {
for (int j = 0; j < 7; j ) {
array2[i][j] = rand() % 8;
}
}
{
for (int i = 0; i < 7; i ) {
for (int j = 0; j < 7; j ) {
addition[i][j] = array1[i][j] array2[i][j];
}
} //add this bracket
for (int i = 0; i < 7; i ) {
for (int j = 0; j < 7; j ) {
cout << array1[i][j];
cout << " " << array2[i][j];
cout << " " << endl;
cout << "both previous numbers added together = " << addition[i][j] << endl;
//} remove this bracket
}
return 0;
}
}
}
Also, to add on, if you want an 8x8 matrix use i<8
and j<8
everywhere in the code. Here you have used i<7 and j<7
which means you get a 7x7 matrix as result.
Logic:
i=0 to i<7
use have => 0,1,2,3,4,5,6
(stopping at 6 because 6<7 is true and 7<7 becomes false naturally). So from 0 to 6
there are totally 7 elements.
Hope this helps! :)