Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am a beginner and I keep getting an error when I am calling my function. I am trying to get the user to input a value for the array and then get it to display it in a table format. Also, I am trying to display a 3x3 matrix in the end.
void DMMatrix(int DM[2][2]){
int number;
cout << "Input 0's and 1's to the DM[2][2]\n\n";
for (int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
cout << "DM[" << i << "]"<< "[" << j << "]: ";
cin >> number;
DM[i][j] = number;
}
}
}
int main(){
int i, j, sum = 0;
int DM[2][2];
DMMatrix(DM[2][2]);
cout << "\n\nOutput (table format)\n\n";
for(i = 0;i < 3;i++){
for(j = 0;j < 3;j++){
cout << " " << DM[i][j] << " ";
}
cout << "\n";
}
sum = DM[0][0] + DM[0][1] + DM[0][2] + DM[1][0] + DM[1][1] + DM[1][2] + DM[2][0] + DM[2][1] + DM[2][2];
if (sum % 2 == 0){
cout << "\nSum is " << sum;
}
}
There are 3 problems in your program.
Mistake 1
You are calling the function DMMatrix incorrectly. The correct way to call DMMatrix would be:
DMMatrix(DM);//CORRECT WAY OF CALLING DMMatrix
Mistake 2
Your 2D array DM has 2 rows and 2 columns and you're accessing 3rd row and 3rd column of the array DM. But since the 3rd row and 3rd column doesn't exist, you have undefined behavior in your program. To solve this you can create a 3x3 2D array as shown below:
#include <iostream>
using namespace std;
void DMMatrix(int DM[3][3]){//2 CHANGED TO 3
int number;
cout << "Input 0's and 1's to the DM[2][2]\n\n";
for (int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
cout << "DM[" << i << "]"<< "[" << j << "]: ";
cin >> number;
DM[i][j] = number;
}
}
}
int main(){
int i, j, sum = 0;
int DM[3][3]; //2 CHANGED TO 3
DMMatrix(DM);
cout << "\n\nOutput (table format)\n\n";
for(i = 0;i < 3;i++){
for(j = 0;j < 3;j++){
cout << " " << DM[i][j] << " ";
sum += DM[i][j];//CALCULATE SUM HERE
}
cout << "\n";
}
cout << "\nSum is " << sum;
}
Mistake 3
You don't need to calculate the sum by writing each element of the array explicitly. You can calculate the sum inside the for loop in my modified above program. Also note that in your original code, while calculating the sum you were going out of bounds which leads to undefined behavior. The above shown program corrected this.
First, the size of the array that you have given to DM is 2x2 but your loop runs for 9 times considering the loop runs for 0, 1 and 2 which will cause a stack smash or Segmentation Fault
#include <iostream>
using namespace std;
void DMMatrix (int DM[3][3])
{
int number;
cout << "Input 0's and 1's to the DM[2][2]\n\n";
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << "DM[" << i << "]" << "[" << j << "]: ";
cin >> number;
DM[i][j] = number;
}
}
}
int main ()
{
int i, j, sum = 0;
int DM[3][3];
DMMatrix (DM);
cout << "\n\nOutput (table format)\n\n";
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
cout << " " << DM[i][j] << " ";
}
cout << "\n";
}
sum = DM[0][0] + DM[0][1] + DM[0][2] + DM[1][0] + DM[1][1] + DM[1][2] +
DM[2][0] + DM[2][1] + DM[2][2];
if (sum % 2 == 0)
{
cout << "\nSum is " << sum;
}
}
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 << " ";
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I need my code to output the common numbers from two arrays but I can't seem to find the issues in my code. Here are some more words so I can fill the quota for stack's post requirement.
Here's the code.
#include <iostream>
using namespace std;
int main()
{
//Variables
int list1[5], list2[5];
cout << "Enter numbers for List No.1" << endl;
for (int i = 1; i <= 5; i++) {
cout << "Enter number " << i << ":";
cin >> list1[i];
}
cout << "Enter numbers for List No.2" << endl;
for (int i = 1; i <= 5; i++) {
cout << "Enter number " << i << ":";
cin >> list2[i];
}
{
cout << "common elements" << endl;
for (int i = 0; list1; i < i++) {
for (int i = 0; list2; i < i++)
if (list1[i] == list2[i]) {
cout << " " << list1[i];
}
}
}
return 0;
}
You should use different counter for each loop and also your loop has wrong parameters. Do it like:
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (list1[i] == list2[j]){
cout<<" "<<list1[i];
}
}
}
Also for initial values start from zero index([0]) as zero index is the first element for each array:
for (int i = 0; i < 5; i++) {
cout << "Enter number " << i << ":";
cin >> list1[i];
}
Another way to do this is by using std::array. It acts very similar to a normal array but it is much better to work with.
for example if you want a char array of length of 10 you can simply write:
std::array<char, 10> My_happy_array.
First please read this to understand why you should not using namespace std. Second consider reading about Range-based for loop
Third, here is the modified version of your program which uses std::array instead of a normal array:
#include <iostream>
#include <array>
int main() {
std::array<int,5> list1;
std::array<int,5> list2;
std::cout << "Enter numbers for List No.1" << std::endl;
for (int i = 0; i < list1.size(); ++i) {
std::cout << "Enter number " << (i+1) << ":";
std::cin >> list1[i];
}
std::cout << "Enter numbers for List No.2" << std::endl;
for (int i = 0; i < list2.size(); ++i) {
std::cout << "Enter number " << (i+1) << ":";
std::cin >> list2[i];
}
for(auto i : list1){
for(auto j : list2){
if(i==j){
std::cout << " " << i;
}
}
}
return 0;
}
Here's fixed code
#include <iostream>
using namespace std;
int main()
{
//Variables
int list1[5] ,list2[5];
cout << "Enter numbers for List No.1" << endl;
for(int i = 0; i < 5; i ++)
{
cout << "Enter number " << i << ":";
cin >> list1[i];
}
cout << "Enter numbers for List No.2" << endl;
for(int i = 0; i < 5; i ++)
{
cout << "Enter number " << i << ":";
cin >> list2[i];
}
cout<<"common elements"<<endl;
for (int i = 0;i<5; i++)
{
for (int j = 0;j<5; j++)
{
if (list1[i] == list2[j])
{
cout<<" "<<list1[i];
}
}
}
return 0;
}
mistakes in code:
you used for loops wrongly whole the time.
array of 5, stores values in index (0,1,2,3,4) not (1,2,3,4,5), so always start loop from 0 and ends before max index (for this case '4' ie. use 'i<5')
while using for loop inside other for loop never use same iterable variable like you used i in both loops use i and j different for each loop,
take a look in my code..
ask me if you get any difficulties.
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
[?] [?] [?]
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
In my code below, I try to find an 'x' in a 2D array and display its row and column. But it always shows the a[0][1] in the result. Can someone tell me where did I wrong and why is that? I tried other code but there is no difference. Thanks
My code:
#include <iostream>
using namespace std;
int main(){
int m, n, i, j, a[m][n], sum, x, sl=0;
cout<<"insert array number of rows: "; cin>>m;
cout<<"insert array number of columns: "; cin>>n;
for(i=0;i<m;i++){
sum=0;
for(j=0;j<n;j++){
cout<<"insert board["<<i<<"]["<<j<<"]: ";
cin>>a[i][j];
sum+=a[i][j];
}
cout<<"the summary of rows "<<i<<" is: "<<sum<<endl;
}
cout<<"search x= "; cin>>x;
for (i=0; i<=m-1; i++){
for (j=0; j<=n-1; j++){
if (a[i][j]==x){
sl++;
cout<<"x is at "<<"a["<<i<<"]["<<j<<"]"<< endl;
}
}
}
if(sl==0){
cout<<"there is no x in the array";
}
cout<<sl;
return 0;
}
My results:
Sources of error in your code:
You were using a variable length array (which is not a part of C++ standard). Moreover you were setting its rows and columns before even initializing/inputting n and m.
Fixed Code:
#include <iostream>
#include <vector>
using namespace std;
int main() {
int m, n, x, sl = 0;
cout << "Insert number of rows in the array: ";
cin >> m;
cout << "\nInsert number of columns in the array: ";
cin >> n;
// put the below line after cin >> m; cin >> n;
vector<vector<int>> a(m, vector<int>(n, 0)); // <-- Use this instead of int a[m][n];
cout << endl;
for (int i = 0; i < m; ++i) {
int sum = 0;
for (int j = 0; j < n; ++j) {
cout << "\nInsert board[" << i << "][" << j << "]: ";
cin >> a[i][j];
sum += a[i][j];
}
cout << "\nThe sum of the row " << i << " is: " << sum << endl;
}
cout << "\nSearch x = ";
cin >> x;
cout << '\n' << endl;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (a[i][j] == x) {
sl++;
cout << "x is at a[" << i << "][" << j << "]\n";
}
}
}
if (sl == 0) { cout << "\nThere is no x in the array!"; }
else cout << '\n' << sl;
return 0;
}
Sample Input :
2
2
3
4
7
3
3
Sample Output :
Insert number of rows in the array:
Insert number of columns in the array:
Insert board[0][0]:
Insert board[0][1]:
The sum of the row 0 is: 7
Insert board[1][0]:
Insert board[1][1]:
The sum of the row 1 is: 10
Search x =
x is at a[0][0]
x is at a[1][1]
2
For interactive run, you can remove the newline characters at your wish.
I wrote a program to accept 15 integer values in an array, then pass this array to a function which will multiply each even index value by 4.
Currently the program displays the initial array, but seems like it's getting hung up before it displays the modified array.
Please help me understand why the program is getting stuck here!
int main(){
const int SIZE = 15;
int quad[SIZE] = {};
void quadruple(int[], const int);
cout << "Enter 15 integer values into an array." << endl;
for (int i = 0; i < SIZE; i++) // Accept 15 int values
{
cout << i << ": ";
cin >> quad[i];
}
cout << "Before quadruple function is called: " << endl;
for (int i = 0; i < SIZE; i++)
{
cout << quad[i] << " ";
}
cout << endl;
quadruple(quad, SIZE);
cout << "After even index value multiplication: " << endl;
for (int i = 0; i < SIZE; i++)
{
cout << quad[i] << " ";
}
cout << endl;
return 0;
}
void quadruple(int values[], const int SZ){
for (int i = 0; i < SZ; i + 2) // Multiply even values by 4
{
if ((i % 2) == 0)
{
values[i] = values[i] * 4;
}
else // Keep odd values the same
{
values[i] = values[i] * 1;
}
}
}
for (int i = 0; i < SZ; i + 2)
"i + 2" doesn't do anything.
You probably meant "i += 2;".
Your homework assignment is to find some documentation about your system's debugger. And find where your rubber duck is, as it's been suggested in the comments.