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
[?] [?] [?]
Related
enter image description here
Hello everyone, I am trying to print a reverse heart shape in c++. While user will input a integer, and the program will display the reverse heart according to the value.
in the photo, the input value are 1,3,4
Now my code can only show the triangle but still missing the two small triangle, so hoping can get some guide here.
int size,space;
cout << "Input size: ";
cin >> size;
int rows = (size * 2)+1;
for (int i = 1, k = 0; i <= rows; ++i, k = 0)
{
for (space = 1; space <= rows - i; ++space)
{
cout << " ";
}
while (k != 2 * i - 1)
{
cout << "* ";
++k;
}
cout << endl;
}
{
for (int i = size; i >= 1; --i)
{
for (int space = 0; space < size - i; ++space)
cout << " ";
for (int j = i; j <= 2 * i - 1; ++j)
cout << "* ";
for (int j = 0; j < i - 1; ++j)
cout << "* ";
cout << endl;
}
}
I try to print a Inverted first, but I fail to print two at the same time.
I want to create a program that take size of rows and columns of 2D array from user, and then take all entries of array from user. And Finally display all the entries from array[0][0] to array[size][size].
My code is:
#include <iostream>
using namespace std;
int main()
{
int rows, columns;
int tom[rows][columns];
cout << "Size of array rows: ";
cin >> rows;
cout << "Size of array columns: ";
cin >> columns;
for(int count1 = 0; count1 < rows; count1++)
{
for(int count2 = 0; count2 < columns; count2++)
{
cout << "Enter entry of row " << count1 << " and column " << count2 << ": ";
cin >> tom[count1][count2];
}
}
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
cout << tom[i][j] << endl;
}
}
return 0;
}
Output is:
Size of array rows: 2
Size of array columns: 3
Enter entry of row 0 and column 0: 1
Enter entry of row 0 and column 1: 2
Enter entry of row 0 and column 2: 3
Enter entry of row 1 and column 0: 12
Enter entry of row 1 and column 1: 13
Enter entry of row 1 and column 2: 14
12
13
14
12
13
14
It should give output:
1
2
3
12
13
14
What is the problem?
Please help.
You can't dynamically create an array like this. This shouldn't even compile. And even if it did, you are creating the array before letting the user input the dimension. For the proper Approach use std::vector:
#include <iostream>
#include <vector>
int main()
{
int rows, columns;
std::cout << "Size of array rows: ";
std::cin >> rows;
std::cout << "Size of array columns: ";
std::cin >> columns;
std::vector<std::vector<int>> tom(rows, std::vector<int>(columns));
for (int count1 = 0; count1 < rows; count1++)
{
for (int count2 = 0; count2 < columns; count2++)
{
std::cout << "Enter entry of row " << count1 << " and column " << count2 << ": ";
std::cin >> tom[count1][count2];
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
std::cout << tom[i][j] << std::endl;
}
}
return 0;
}
output:
Size of array rows: 2
Size of array columns: 3
Enter entry of row 0 and column 0: 1
Enter entry of row 0 and column 1: 2
Enter entry of row 0 and column 2: 3
Enter entry of row 1 and column 0: 12
Enter entry of row 1 and column 1: 13
Enter entry of row 1 and column 2: 14
1
2
3
12
13
14
please don't use using namespace std; - read here why.
You initialized tom before actually getting the number of rows/columns.
#include <iostream>
int main()
{
int rows, columns;
std::cout << "Rows: ";
std::cin >> rows;
std::cout << "Columns: ";
std::cin >> columns;
int arr[rows][columns];
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
std::cout << "Enter the value for [" << i << "][" << j << "] : ";
std::cin >> arr[i][j];
}
}
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
std::cout << arr[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
You cannot declare an array like this:
int tom[rows][columns];
since values of rows and columns are not known yet.
Instead you should initialize this array dynamically after asking for values.
Here is your code fixed:
#include <iostream>
using namespace std;
int main()
{
int rows, columns;
int **tom;
cout << "Size of array rows: ";
cin >> rows;
cout << "Size of array columns: ";
cin >> columns;
tom = new int*[rows];
for (int i = 0; i < rows; i++) {
tom[i] = new int[columns];
}
for(int count1 = 0; count1 < rows; count1++)
{
for(int count2 = 0; count2 < columns; count2++)
{
cout << "Enter entry of row " << count1 << " and column " << count2 << ": ";
cin >> tom[count1][count2];
}
}
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
cout << tom[i][j] << endl;
}
}
return 0;
}
Lets suppose we have a 5 X 5 random array
1 2 3 7 8
4 7 3 6 5
2 9 8 4 2
2 9 5 4 7
3 7 1 9 8
Now I want to print the right side of the diagonal shown above, along with the elements in the diagonal, like
----------8
--------6 5
------8 4 2
---9 5 4 7
3 7 1 9 8
The code I've written is
#include <iostream>
#include <time.h>
using namespace std;
int main(){
int rows, columns;
cout << "Enter rows: ";
cin >> rows;
cout << "Enter colums: ";
cin >> columns;
int **array = new int *[rows]; // generating a random array
for(int i = 0; i < rows; i++)
array[i] = new int[columns];
srand((unsigned int)time(NULL)); // random values to array
for(int i = 0; i < rows; i++){ // loop for generating a random array
for(int j = 0; j < columns; j++){
array[i][j] = rand() % 10; // range of randoms
cout << array[i][j] << " ";
}
cout << "\n";
}
cout << "For finding Max: " << endl;
for(int i = 0; i < rows; i++){//loop for the elements on the left of
for(int j = columns; j > i; j--){//diagonal including the diagonal
cout << array[i][j] << " ";
}
cout << "\n";
}
cout << "For finding Min: " << endl;
for(int i = rows; i >= 0; i++){ //loop for the lower side of
for(int j = 0; j < i - columns; j++){ //the diagonal
cout << array[i][j] << " ";
}
cout << "\n";
}
return 0;
}
After running the code the shape I get is correct , but the elements do not correspond to the main array. I have no idea what the problem is.
Left side:
for (size_t i = 0; i < rows; i++) {
for(size_t j = 0; j < columns - i; j++) {
cout << array[i][j] << " ";
}
cout << "\n";
}
Right side:
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < columns; j++) {
if (j < columns - i - 1) cout << "- ";
else cout << vec[i][j] << " ";
}
cout << "\n";
}
My task is to replace elements of vector and elements on diagonal matrix. Vector is entered by user and the matrix is random. For example, I write vector:
1 2 3
And the random matrix is
7 0 0
0 3 0
0 0 8
I must get this
1) 7 3 8
2)
1 0 0
0 2 0
0 0 3
The first part of it i got, but the second I'm stacked. Here is entering vector:
int size;
std::cout << ("Enter the dimentoinal of vector and matrix (enter one number): ");
std::cin >> size;
int * arrayPtr = (int*) calloc(size,sizeof(int));
if (arrayPtr == NULL) exit (1);
for (int ix = 0; ix < size; ix++)
{
std::cout << "Enter element #" << ix<< " ";
std::cin >> arrayPtr[ix];
}
system ("clear") ;
std::cout << "\n\nResulting vector is:\n[ ";
for (int ix = 0; ix < size; ix++)
{
std::cout << arrayPtr[ix] << " ";
}
cout << "]\n\n\n" ;
Here is code, that not working(on the screen is not correct result):
cout << "The new matrix is :\n" ;
int * matr_n = (int*) calloc(size,sizeof(int));
cout << "\n" ;
for (int ix = 0 ; ix<size; ix++)
{
matr_n = &arrayPtr[ix] ;
cout << *matr_n << " " ;
for (int i = 0; i<size; i++)
{
cout << "\n" ;
for (int j = 0; j<size; j++)
{
if (i==j)
{
cout << *matr_n << " " ;
}
else
cout << 0 << " " ;
}
}
}
I know that the problem is in using pointers or malloc/calloc function, but for beginner is hard to catch it fast.
Can you fix it, please?
The inner for-loop which prints the matrix should be separated from the loop which calculates its values.
In other words, you'll need to change the bottom part of the code as follows:
for (int ix = 0; ix < size; ix++)
{
matr_n = &arrayPtr[ix];
std::cout << *matr_n << " ";
}
for (int i = 0; i<size; i++)
{
std::cout << "\n";
for (int j = 0; j<size; j++)
{
if (i == j)
{
std::cout << *matr_n << " ";
}
else
std::cout << 0 << " ";
}
}
So, I am trying to implement this algorithm from our textbook.
I wrote this :
// Knapsack_memoryfunc.cpp : Defines the entry point for the console application.
//Solving Knapsack problem using dynamic programmig and Memory function
#include "stdafx.h"
#include "iostream"
#include "iomanip"
using namespace std;
int table[20][20] = { 0 };
int value, n, wt[20], val[20], max_wt;
// ---CONCERNED FUNCTION-----
int MNSack(int i, int j)
{
value = 0;
if (table[i][j] < 0)
if (j < wt[i])
value = MNSack(i - 1, j);
else
value = fmax(MNSack(i - 1, j), val[i] + MNSack(i - 1, j - wt[i]));
table[i][j] = value;
return table[i][j];
}
// --------------------------
void items_picked(int n, int max_wt)
{
cout << "\n Items picked : " << endl;
while (n > 0)
{
if (table[n][max_wt] == table[n - 1][max_wt]) // if value doesnot change in table column-wise, item isn't selected
n--; // n-- goes to next item
else // if it changes, it is selected
{
cout << " Item " << n << endl;
max_wt -= wt[n]; // removing weight from total available (max_wt)
n--; // next item
}
}
}
int main()
{
cout << " Enter the number of items : ";
cin >> n;
cout << " Enter the Maximum weight : ";
cin >> max_wt;
cout << endl;
for (int i = 1; i <= n; i++)
{
cout << " Enter weight and value of item " << i << " : ";
cin >> wt[i] >> val[i];
}
for (int i = 0; i <= n; i++)
for (int j = 0; j <= max_wt; j++)
table[i][j] = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= max_wt; j++)
table[i][j] = -1;
cout << " Optimum value : " << MNSack(n, max_wt);
cout << " \n Table : \n";
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= max_wt; j++)
if (table[i][j] == -1)
cout << setw(5) << "-";
else
cout << setw(5) << table[i][j];
cout << endl;
}
items_picked(n, max_wt);
return 0;
}
Here is the question and output :
It seems like its correct on some places like optimum value, yet isn't fully acceptable.
I've tried to debug it, but its quite hard with recursive functions. Can someone please help?
int MNSack(int i, int j)
{
value = 0;
if (table[i][j] < 0)
{
if (j < wt[i])
value = MNSack(i - 1, j);
else
value = max(MNSack(i - 1, j), val[i] + MNSack(i - 1, j - wt[i]));
table[i][j] = value;
}
return table[i][j];
}
The problem comes in here. When your table item is greater or equal to 0, you will skip the recursion but still set the table item to 0, which won't be right if your table item is greater than 0.
You only need to update the table item when it needs to be change, so put it in the braces will correct this.
The bottom up solution.
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
int main()
{
int table[20][20] = { 0 };
int value, n, wt[20], val[20], max_wt;
cout << " Enter the number of items : ";
cin >> n;
cout << " Enter the Maximum weight : ";
cin >> max_wt;
cout << endl;
for (int i = 1; i <= n; i++)
{
cout << " Enter weight and value of item " << i << " : ";
cin >> wt[i] >> val[i];
}
// Initialization
for (int i = 0; i <= n; i++)
for (int j = 0; j <= max_wt; j++)
table[i][j] = 0;
// In practice, this can be skipped in a bottom up solution
for (int i = 1; i <= n; i++)
for (int j = 1; j <= max_wt; j++)
table[i][j] = -1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= max_wt; j++)
{
if (j < wt[i])
table[i][j] = table[i - 1][j];
else
table[i][j] = max(table[i - 1][j], val[i] + table[i - 1][j - wt[i]]);
}
}
cout << " Optimum value : " << table[n][max_wt] << endl;
cout << " \n Table : \n";
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= max_wt; j++)
if (table[i][j] == -1)
cout << setw(5) << "-";
else
cout << setw(5) << table[i][j];
cout << endl;
}
return 0;
}
You can see that this changes the recursion to a loop, and therefore avoids the global variables. It also makes the code simpler, so that you can avoid checking if the table item is valid (equal to -1 in your example).
The drawback of this solution is, it always traverses all the possible nodes. But it gains better coefficient per item because the recursion and double checking the table item costs more. Both top-down and bottom-up have the same order of complexity O(n^2), and it's hard to tell which one is faster.