How to add two binary numbers in the array c++ - 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?

Related

nested For loop pattern in c++

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

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

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

CodeChef Error in submission(Not asking the solution)

I completed my solution for SUMTRIAN i.e. https://www.codechef.com/problems/SUMTRIAN
When I submit, it gives me wrong answer. I am definite that I have formatting problems and I am not sure how do I print my answer for that.
here is the code -
#include<bits/stdc++.h>
using namespace std;
int main()
{int w;
cin>>w;
for(int x=0;x<w;x++)
{
int a[105][105],n;
cin>>n;
int i,j;
for( i=0;i<n;i++)
for( j=0;j<=i;j++)
cin>>a[i][j];
i = n-1;
j = n-1;
int sumarr[n],sum=0;
for(int z=0;z<n;z++)
{
i=n-1;
j=n-1-z;
sum=0;
do{
sum+=a[i][j];
// cout<<a[i][j]<<" ";
i--;j--;
if(j<0)
j++;
}while(i>=0 && j>=0);
sumarr[z]=sum;
// cout<<endl;
}
// cout<<sumarr[1];
cout<<*max_element(sumarr,sumarr+n)<<"\n";
}
}
Any help would be great! :D
Thanks in advance !!:)

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.

Another way to iterate a matrix using a pointer and extract certain values

What i was trying to do with this code was to iterate a symmetrical matrix and count how many negative numbers are above the main diagonal.The output is a random interminable series of numbers. Could anyone please tell me either another way to iterate a matrix using a pointer or where did i do wrong in this one?
#include <iostream>
#include <conio.h>
using namespace std;
void read_array(int *p,int n);
int neg(int *p,int n);
void display(int *p,int n);
void main(){
int a[20][20],n,*p=&a[0][0];
cout<<"How many rown and columns would you like to have?"<<endl;
cin>>n;
cout<<"Insert the numbers in the array"<<endl;
read_array(p,n);
display(p,n);
cout<<"The number of negative numbers above the main diagonal"<<endl;
neg(p,n);
}
void read_array(int *p,int n){
for (int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>*(p+i*n+j);
}
}
}
int neg(int *p,int n){
int count=0;
for (int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(*(p+i*n+j)<0){
count++;
}
}
}
return count;
}
void display(int *p,int n){
for(int i=0;i<n;i++){
for(int j=0;i<n;j++){
cout<< *(p+i*n+j)<<" ";
}
}
}
int neg(int * p, int n) {
int count = 0;
for(int i = 0; i < n; i++)
for(int j = i + 1; j < n; j++)
if(*(p + i * n + j) < 0)
count++;
return count;
}
void display(int *p,int n){
for(int i=0;i<n;i++){
for(int j=0;i<n;j++){
cout<< *(p+i*n+j)<<" ";
}
}
}
There is a typo, it should be j<n in for(int j=0;i<n;j++){.
Also your code does not exactly do what you explained in the question. The matrix is not symmetrical and you count through the whole thing not only the upper half.
Your code will behave undefined for inputs n > 20. You should use a std::vector<int> instead, which you can dynamically adjust for the input. Pointers are not really necessary either. Just pass the vector by reference or the array as array and index them in the function like normal arrays.