EDIT: I'm fairly new to c++. Began working with this language two weeks ago.
Sorry if this have been asked before, but i have searched everywhere on the web on how to sum individual rows in a 2D array and not found the answer that i was looking for.
i need to display the sum of each individual row in a[m][n], but for some reason this only works when my array is 2x2, but if it's 3x3 or bigger, then i get the following output in the terminal:
for intsance, a[3][3]=
{1,2,3}, //this is determined by the user
{1,2,3},
{1,2,3};
then i get the following output:
9179942 //address of some sort???
6 // actual sum. code is working here (yay :D)
469090925// again something i dont understand
This is what i have so far
#include <iostream>
using namespace std;
int main(){
int m,n;
cout<<"Enter number of rows for array"<<endl;
cin>>m;
if (m>10){
cout<<"More than 10 rows will be too big"<<endl;
return 1;
}
cout<<"Enter number of collumns for array"<<endl;
cin>>n;
if (n>10){
cout<<"More than 10 collumns will be too big"<<endl;
return 1;
}
int a[m][n];
for(int i=0; i<m;i++){
cout<<"Enter "<<m<<" values into row "<<i+1<<endl;
for(int j=0; j<n; j++){
cout<<"a ["<<i<<"]["<<j<<"]: ";
cin>>a[i][j];
}
}
cout<<"Array dimensions: "<<m<<"x"<<n<<'\n'<<"resulting array: "<<endl;
for(int i=0; i<m;i++){
for(int j=0; j<n; j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
int avg[m];
int el_less_avg;
for(int i=0; i<m; i++){
for(int j=0; j<n;j++){
avg[i]+=a[i][j];
}
}cout<<"\n\n";
for(int i=0; i<m; i++){
cout<<avg[i]<<endl;
}
return 0;
}
int avg[m];
int el_less_avg;
for(int i=0; i<m; i++){
for(int j=0; j<n;j++){
You're not initializing these values so they're free to be whatever cruft is on the stack at the time. You need to initialize them.
int avg[m];
for (int i = 0; i < m; ++i) {
avg[i] = 0;
for (int j = 0; j < n; ++j) {
avg[i] += a[i][j];
}
}
int a[m][n]; is not allowed in Standard C++. The dimensions of C-style arrays must be known at compile-time. A program using this code could do literally anything.
You could replace this line with:
vector<vector<int>> a(m, vector<int>(n));
which seems like a mouthful at first, but you will find it makes your problem go away.
Another bonus of this approach is that you can then use range-based loops:
for(int x : avg)
cout << x << endl;
which reduces the chance of making an error by using the wrong letter in the loop condition.
Related
in c++, when I am making nested For loop and I am trying to calculate the factorial...I don't get the correct factorials...I don't know why. For example factorial of 5 is 120 but here it results in 34560. why?
and here is the code:
int fact=1;
for (int number=1; number<=10; number++) {
for (int i=1; i<=number; i++)
fact=fact*i;
cout <<"factorial of "<<number<<"="<<fact<<"\n";
}
here is it pictured:
You need to re-initialize fact for each number.
int fact=1;
for (int number=1; number<=10; number++) {
fact = 1;
for (int i=1; i<=number; i++)
fact=fact*i;
cout <<"factorial of "<<number<<"="<<fact<<"\n";
}
Doing some simple exercies with C++ and I'm stuck...
When my sum[t] array is declared as integer or float it sometimes outputs at the end some crazy values like for example 4239023 or -3.17802e+30 (despite the fact that I add only small numbers from range <-100; 300>). When I change it for double it works correctly. Why int and float don't work here?
#include <iostream>
using namespace std;
int main()
{
int t=0;
int n=0;
int x=0;
cout<<"Amount of sums: ";
cin>>t;
int sum[t];
for (int i=0; i<t; i++)
{
cout<<"Sum no. "<<i+1<<". How many numbers you wish to add: ";
cin>>n;
for (int j=0; j<n; j++)
{
cout<<"Insert number "<<j+1<<" : ";
cin>>x;
sum[i]+=x;
}
}
for (int i=0; i<t; i++)
{
cout<<sum[i]<<endl;
}
return 0;
}
You should initialize your array of sums to zeroes after you receive the value of t. You could do it like this:
for (int i=0; i<t; i++)
{
sum[i] = 0;
}
I think I made a mistake somewhere but I can't seem to find it. I think the problem lies in adding or entering data in the wrong order. I apologize for any mistakes, English is not my primary language. enter image description here
#include <iostream>
using namespace std;
int main()
{
int d, n, m, carry;
int a[10000];
int b[10000];
int addition[10001];
cin>>d;
for(int i=0; i<d; i++)
{
int a[10000]={0};
int b[10000]={0};
int addition[10001]={0};
cin>>n;
for(int i=n; i>=1; i--)
{
cin>>a[i];
}
cin>>m;
for (int i=m; i>=1; i--)
{
cin>>b[i];
}
if(n<m)
{
n=m;
}
carry=0;
for (int i=1; i<=n; i++)
{
addition[i]=(a[i]+b[i]+carry)%2; //way my teacher
carry=(a[i]+b[i]+carry)/2;
}
addition[n+1]=carry;
// if(addition[n+1]==0)n--;
for(int i=n; i>=0; i--)
{
cout<<addition[i];
}
}
return 0;
}
I think there are at least two errors.
1) In
for(int i=n; i>=0; i--)
you're counting down to 0. But in every other loop, you've counted down to 1. Since you're counting down too far, your output would have an extra zero at the end. (For example, it would show 1000 when it should have shown 100.)
2) Also in that loop, you're starting at n. But you've potentially put a carry into n+1, aren't you forgetting to output it too?
I wrote this program for the college. The user must input 2 numbers (n,m) and the program must make an array with the size [n,m]. Then the user will fill the array and the program must find the maximum number of the columns and print the minimum of them. Then it must find the minimum number of the lines and print the maximum. The compiler says 'n' was not declared in this scope and i don't know why.
Can you help me?
Thank's in advance.
#include <iostream>
using namespace std;
void max (int pinakas[n,m]) {
int i,j,max,pinm[m],min;
for (j=0; j<m; j++){
max=pinakas[0,j];
for (i=0; i<n; i++)
if (pinakas[i,j]>max)
max=pinakas[i,j];
pinm[j]=max;
}
min=pinm[0];
for (j=0; j<m; j++)
if (pinm[j]<min)
min=pinm[j];
cout << min;
}
void min (int pinakas[n,m]) {
int i,j,max,pinm[n],min;
for (i=0; i<n; i++){
min=pinakas[i,0];
for (j=0; j<m; j++)
if (pinakas[i,j]<min)
min=pinakas[i,j];
pinm[i]=min;
}
max=pinm[0];
for (i=0; i<n; i++)
if (pinm[i]>max)
max=pinm[i];
cout << max;
}
int main (){
int n,m,i,j;
cin >> n >> m;
int pin[n,m];
for (i=0; i<n; i++)
for (j=0; j<m; j++)
cin >> pin[i,j];
max(pin[n,m]);
min(pin[n,m]);
return 0;
}
You have tones of errors on your code. I suggest you to give a look at parameters in functions and variable declarations.
hope you doing well.
so i have just started to do an assignment and the first thing i wanted to do was to create the two dynamic array. however, there is something wrong with the array i can't assign values to it. Here is the code:
void Room::memory(int **array){
int x,x2;
int count=0;
cout << "Array size? rows: columns: \n";
cin >> x >> x2;
array = new int*[x];
for(int i=0; i<x;i++){
array[i]= new int[x2];
}
for(int i=0; i<x; i++){
for(int j=0; j<x2; j++){
array[i][j]=count;
count++;
}
}
for(int i=0; i<x;i++){
array[i]= new int[x2];
}
for(int i=0; i<x; i++){
for(int j=0; j<x2; j++){
cout<< array[i][j]<< " | ";
}
cout << endl;
}
}
i always get the value 0 for my array. whether i use this line or not:
array[i][j]=count;
i tied to compare my code with someone else and it is the same steps but it doesn't work for me.
class Room{
private:
int **array;
public:
void memory(int **array);
};
Why do you do
for(int i=0; i<x;i++){
array[i]= new int[x2];
} twice?
at the same time, please change void memory(int **array); to void memory();
while you crested the 2nd 2D dynamic array,you did not initialize it...And just printed it... In the 2nd 2D array you also need to initialize before printing the all index values....
for(int i=0; i<x;i++){
array[i]= new int[x2];
}
//here should be the initialization
for(int i=0; i<x; i++){
for(int j=0; j<x2; j++){
cout<< array[i][j]<< " | ";
}
cout << endl;
}