I am currently working on a program that prints a 5 variable truth table. I am using a 2d array. My code currently produces the table, but says it is corrupt and "the stack around the variable "table" was corrupted. Any help?
#include <iostream>
using namespace std;
int main() {
bool table[5][32];
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
table[i][j] = ((i >> j)& 1);
}
}
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
cout << table[i][j] << " ";
}
cout << endl;
}
return 0;
}
This is homework, so I would like to understand it, not just have an answer.
The index is wrong. Only table[0] to table[4] are available, so accessing table[5] to table[31] is illegal.
Try this:
#include <iostream>
using namespace std;
int main() {
bool table[32][5]; // swap 32 and 5
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
table[i][j] = ((i >> j)& 1);
}
}
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
cout << table[i][j] << " ";
}
cout << endl;
}
return 0;
}
There is attempt to read out of bound values from array.
If you need 5x32 matrix Use code below:
for (int i = 0; i < 5; i++) { // 32-> 5
for (int j = 0; j < 32; j++) { // 5->32
If you need 32x5 matrix then replace code below:
bool table[32][5]; //it was table[5][32];
Related
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;
}
}
}
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.
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! :)
What's wrong with my code
I Have to add all the edge values of the 2D Array.
The Function is not returning what I want. Its returning garbage value.
Program Req Output : Add all the edges value of the 2D Array and return its answer.
You are not allowed to use Vector etc.
Here is my Code :
#include <iostream>
using namespace std;
const int rows = 3;
const int cols = 4;
void AddEdges (int arr[rows][cols])
{
int sum = 0;
int totalOfEdges = 0;
int i,j;
int k = 0;
int l = 0;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= cols; j++)
{
if (i == 1 || i == rows || j == 1 || j == cols)
{
sum = arr[k][l];
totalOfEdges = sum + totalOfEdges;
}
l++;
}
k++;
}
cout << "Total of Edges = " << totalOfEdges << endl;
}
int main()
{
int arr[3][4] = {0};
cout << "Enter Elements for Array = \n";
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cin >> arr[i][j];
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
AddEdges(arr);
}
My Output :
Leaving the Correct Code in Case Any one need it .
#include <iostream>
using namespace std;
const int rows = 3;
const int cols = 4;
void AddEdges (int arr[rows][cols])
{
int sum = 0;
int totalOfEdges = 0;
int i,j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
if (i == 0 || i == (rows-1) || j == 0 || j == (cols-1))
{
sum = arr[i][j];
totalOfEdges = sum + totalOfEdges;
}
}
}
cout << "Total of Edges = " << totalOfEdges << endl;
}
int main()
{
int arr[3][4] = {0};
cout << "Enter Elements for Array = \n";
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cin >> arr[i][j];
}
}
cout << "--------" << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
AddEdges(arr);
}
So, I'm trying to swap the matrix elements with respect to the main diagonal. I have tried using temp method (switching values while using temp variable), also tried std::swap(a,b). Somehow it only swaps upper right side of the matrix and leaves another half not changed.
How do I make everything swap?
My code:
#include <iostream>
using namespace std;
int main()
{
const int n = 7;
srand (time(NULL));
int matrix[n][n];
cout << "Original Matrix :" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
(i == j) ? matrix[i][j] = 0 : matrix[i][j] = rand() % 100+1;
cout << matrix[i][j] << "\t";
}
cout << endl;
}
cout << "\nRemade Matrix:" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
// swap(matrix[i][j], matrix[j][i]); //another method
cout << matrix[i][j] << "\t";
}
cout << endl;
}
return 0;
}
You are basically swapping them twice, replace your swapping loops with this. Notice the condition of the second loop, it's j < i. Then, you can print it with another set of loops.
for (int i = 0; i < n; i++)
for (int j = 0; j < i ; j++)
swap(matrix[i][j], matrix[j][i]);
Your logic is almost ok. Just the inner loop counter will start from "i+1" else after the swapping the value is again getting overlapped. Try the following code and make sure you understand it. Happy coding!
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << matrix[i][j] << "\t";
}
cout << endl;
}
Hey I am beginner at C++ programming. I have made a program that is meant to add two 2D arrays together. However, The program outputs the values until the program crashes. Can someone help me to identify the problem?
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int a[10][10], c[10][10], i, j;
for (i = 1; i <= 10; ++i)
{
for(j=0; j < 10; ++j)
{
a[i][j] = i * j;
}
}
// We are able to treat the individual columns as arrays
for (int i = 0; i < 10; ++i)
{
int *b = a[i];
for (int j = 0; j < 10; ++j)
{
cout << b[j] << " ";
}
cout << endl;
}
cout << "****" << endl;
// Declare a multidimensional array on the heap
int **b = new int*[10];
// need to allocate all members individually
for (int i = 0; i < 10; ++i)
{
b[i] = new int[10];
}
// Set the values of b
for (int i = 0; i < 10; ++i)
{
for (j = 0; j < 10; ++j)
{
b[i][j] = (i * 10) + j;
}
}
for (i = 0; i < 10; ++i)
{
for (j = 1; j <= 10; ++j)
{
c[i][j] = a[i][j] + b[i][j];
}
}
for (i = 0; i < 10; ++i)
{
for (j = 1; j <= 10; ++j)
{
cout << c[i][j] << endl;
}
}
// Delete the multidimensional array - have to delete each part
for (int i = 0; i < 10; ++i)
{
delete[] b[i];
}
delete[] b;
return 0;
}
I corrected your code.Now, It's working and program didn't crash. You can try it out.
#include<conio.h>
#include<iostream.h>
int main(int argc, char** argv)
{
int a[10][10], c[10][10], i, j;
for (i = 0; i <10; ++i)
{
for(j=0; j < 10; ++j)
{
a[i][j] = i * j;
}
}
//We are able to treat the individual columns as arrays;
for (i = 0; i < 10; ++i)
{
int *b = a[i];
for (int j = 0; j < 10; ++j)
{
cout << b[j] << " ";
}
cout << endl;
}
cout << "****" << endl;
//Declare a multidimensional array on the heap;
int **b = new int*[10];
//need to allocate all members individually
for (i = 0; i < 10; ++i)
{
b[i] = new int[10];
}
//Set the values of b
for ( i = 0; i < 10; ++i)
{
for (j = 0; j < 10; ++j)
{
b[i][j] = (i * 10) + j;
}
}
for (i = 0; i < 10; ++i)
{
for (j = 0; j <10; ++j)
{
c[i][j] = a[i][j] + b[i][j];
}
}
for (i = 0; i < 10; ++i)
{
for (j = 0;j < 10; ++j)
{
cout << c[i][j] << " ";
}
cout<<endl;
}
// Delete the multidimensional array - have to delete each part
for (i = 0; i < 10; ++i)
{
delete[] b[i];
}
delete[] b;
return 0;
}
I have this function meant to initialize a multidimensional 2d (6x6) array to zero. I call the function in main using cout to test it and it outputs garbage. Please help. Thanks!
int** initializeArray(void)
{
typedef int* rollArray; //this line is actually outside of the function in my
//program
int i, j;
rollArray *m = new rollArray[6];
for (i = 0; i < 6; i++)
m[i] = new int[6];
for (i = 0; i < 6; i++)
for (j = 0; j < 6; j++)
m[i][j] = 0;
return m;
}
If the value 6 is known at compile-time, I would suggest using std::array in a nested fashion. For example:
#include <array>
#include <iostream>
int main()
{
std::array<std::array<int,6>,6> a = {0};
for (int i = 0; i < 6; ++i)
{
for (int j = 0; j < 6; ++j)
{
std::cout << a[i][j] << std::endl; // Prints 0.
}
}
return 0;
}
In fact, you won't even need to create a function to initialize your array. Declare your nested array and you are good to go. (If you don't know the dimension at compile-time, you could use std::vector in a similar fashion.)
The problem is with your test.
How can you mess up such a simple test? Just use:
int ** a = initializeArray();
int i,j;
for (i = 0; i < 6; i++) {
for (j = 0; j < 6; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}