Int and Float arrays give wrong results after adding numbers using loops - c++

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;
}

Related

Adding values of 2 columns from a matrice and putting the sum in a vector

My first question about CPP in Stackoverflow. I hope i get an answer.
#include <fstream>
using namespace std;
ifstream fin("Matr.dat");
ofstream fout("out.dat");
int a[1000][1000];
int b[10000];
int n, m;
void read()
{
fin>>n>>m;
for(int i=1; i<n; i++)
{
for(int j=1; j<m; j++)
fin>>a[i][j];
}
}
int vect()
{
int c=1;
for(int i=2; i<=n; i++)
{
for(int j=2; j<=m; j++)
{
b[c]=a[j]+a[j+1];
c++;
}
}
return c;
}
int main()
{
read();
int c=vect();
for(int i=0; i<c; i++)
fout<<b[i]<<' ';
return 0;
}
I and a colleague are trying to add the values of 2 columns into a vector.
For the line `b[c]=a[j]+a[j+1]; i receive an error saying that i use 2 incompatible types together. It is not working...
Can someone please help me add the values of 2 columns into a vector?

How to add two binary numbers in the array c++

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?

How do i output sum of rows of a 2D array C++

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.

For loop don't work properly for more than one array in C++

I have ruby version of this program & trying to do the same in C++
The input must be:
2 # Number of pairs
562 -881 # First pair
310 -385 # Second pair
And output:
-319
-75
It's working fine with one array of 2 numbers and breaks if pairs > 2. What's wrong in my for loops?
#include <iostream>
using namespace std;
int main() {
int sum = 0;
int iter;
cin >> iter;
int arr[2];
for (int i=0; i<iter; i++) {
for (int n=0; n<2; n++) {
// Enter numbers
cin >> arr[n];
}
}
for (int num=0; num<2; num++) {
sum+=arr[num];
}
for (int i=0; i<iter; i++) {
// Get the sum of numbers
cout << sum << endl;
}
return 0;
}
Thanks for any help!
for (int i=0; i<iter; i++) {
for (int n=0; n<2; n++) {
// Enter numbers
cin >> arr[n];
}
}
In first iteration values are entered in arr and again in second iteration previous values are overwritten (similar in next iterations if any ). This is the problem .
Solution -
#include <iostream>
using namespace std;
int main() {
int iter;
cin >> iter;
int arr[2];
int sum[iter]; // declare sum as array with iter number of elements
for(int i=0;i<iter;i++){
sum[i]=0; // initialize elements of sum to 0
}
for (int i=0; i<iter; i++) {
for (int n=0; n<2; n++) {
// Enter numbers
cin >> arr[n]; // take iput
sum[i]+=arr[n]; // add numbers and store in sum
}
}
for (int i=0; i<iter; i++) {
// Get the sum of numbers
cout << sum[i] << endl; // print values in sum after taing all inputs
}
return 0;
}
You're overwriting the contents of arr on each iteration of the loop. Try something like this (live demo here):
#include <iostream>
using namespace std;
int main() {
int sum = 0;
int iter;
cin >> iter;
int arr[2];
for (int i=0; i<iter; i++) {
for (int n=0; n<2; n++) {
// Enter numbers
cin >> arr[n];
}
for (int num=0; num<2; num++) {
sum+=arr[num];
}
cout << sum << endl;
}
return 0;
}

two-dimensional array not displaying proper output

i want the user to input values in a 2 dimensional array. he can also choose the size of each dimension
int main()
{
int x;
int y;
int *p;
cout<<"How many items do you want to allocate in dimension x?"<<endl;
cin>>x;
cout<<"How many items do you want to allocate in dimension y?"<<endl;
cin>>y;
p = new int[x,y];
for(int i=0; i<x; i++) //This loops on the rows.
{
for(int j=0; j<y; j++) //This loops on the columns
{
int value;
cout<<"Enter value: "<<endl;
cin>>value;
p[i,j] = value;
}
}
drill1_4 obj;
obj.CopyArray(p,x,y);
}
and then, ill output the two dimensional array via
class drill1_4
{
public:
void CopyArray(int*,int,int);
private:
int *p;
};
void drill1_4::CopyArray(int* a,int x,int y)
{
p = a;
for(int i=0; i<x; i++) //This loops on the rows.
{
for(int j=0; j<y; j++) //This loops on the columns
{
cout << p[i,j] << " ";
}
cout << endl;
}
getch();
}
logic seems fine, but lets say, if the user enters the numbers, the array should look like this
1 2
3 4
but instead, it looks like this:
3 3
4 4
The array is not displaying it correctly.
I don't know if you figured out the answer to your problem. The comments above tell you where you went wrong. Here is a possible answer.
#include <cstdio>
#include <iostream>
using namespace std;
class drill1_4
{
public:
void CopyArray(int**,int,int);
private:
int **p;
};
void drill1_4::CopyArray(int** a,int x,int y)
{
p = a;
for(int j=0; j<y; ++j) //This loops on the rows.
{
for(int i=0; i<x; ++i) //This loops on the columns
{
cout << p[i][j] << " ";
}
cout << endl;
}
cin.get();
}
int main()
{
int x;
int y;
int **p;
cout<<"How many items do you want to allocate in dimension x?"<<endl;
cin>>x;
cout<<"How many items do you want to allocate in dimension y?"<<endl;
cin>>y;
p = new int*[x];
for (size_t i = 0; i < x; ++i)
p[i] = new int[y];
for(int j=0; j<y; ++j) //This loops on the rows.
{
for(int i=0; i<x; ++i) //This loops on the columns
{
int value;
cout<<"Enter value: "<<endl;
cin>>value;
p[i][j] = value;
}
}
drill1_4 obj;
obj.CopyArray(p,x,y);
}
I am not sure I understood what you meant by x-dimmension. If you meant x running horizontally than I think that i and j for-loops shout be reversed as x will represent columns.