ive been trying to add 2 large integer on c++ using array and the output is always 0000 and i cant identified the problem
what im trying to achieve is like 30000000000 + 50000000 = 30050000000.
and this is the code that i wrote
, can anyone help me trying to figure it out why the output is 0000?
#include <math.h>
using namespace std;
int array1[100];
int array2[100];
int hasil[100];
int func1(int n)
{
int b;
while(b>0)
b=sizeof(array1[100]);
array1[b]=n%10;
n=n/10;
b--;
}
int func2(int p)
{
int b;
while(b>0)
b=sizeof(array1[100]);
array2[b]=p%10;
p=p/10;
b--;
}
int func3(int i)
{ int satuan,puluhan,bil;
for(i=sizeof(array2[100]);i>0;i--)
{
int bil = array1[100]+array2[100];
satuan=bil%10;
hasil[i]=satuan+puluhan;
puluhan=bil/10;
}
}
int main ()
{ int n,p,i;
int func1(n);
int func2(p);
int func3(i);
int satuan=0;
int puluhan=0;
int x,y;
cout<<"masukan bilangan pertama = ";
cin>>n; cout<<endl;
cout<<"masukan bilangan ke dua = ";
cin>>p; cout<<endl;
for(x=sizeof(hasil[100]);x>0;x--)
{
y=0;
cout<<hasil[y];
y++;
}
} ```
Related
What is problem with my code for variable size multidimensional array .How to fix this problem.
My code is not passing all test cases.Can anyone help me to fix it.This is question from hackerrank challenge.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,q;
cin>>n;
cin>>q;
int *arr[n];
for(int i=0;i<n;i++)
{
int x;
cin>>x;
int b[x];
for(int j=0;j<x;j++)
{
cin>>b[j];
}
arr[i]=b;
}
while(q--)
{
int i,e;
cin>>i>>e;
cout<<arr[i][e]<<endl;
}
return 0;
}
Here is correct code.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,q;
cin>>n;
cin>>q;
int *arr[n]; // passed all test cases
for(int i=0;i<n;i++)
{
int x;
cin>>x;
int *b=new int[x];
for(int j=0;j<x;j++)
{
cin>>b[j];
}
arr[i]=b;
}
while(q--)
{
int i,e;
cin>>i>>e;
cout<<arr[i][e]<<endl;
}
return 0;
}
MY CODE along with the output
The following code is not working. It has no errors, am doing some mistake in the logic I think. I want to find power of a number using functions. How to make this code work?
The code:
#include<iostream>
using namespace std;
int pow(int);
int main()
{
int x,p,ans;
cout<<"Enter a number";
cin>>x;
cout<<"Enter the power of the number";
cin>>p;
ans=pow(x);
cout<<ans;
return 0;
}
int pow(int)
{
int a=1,i,p,x;
for(i=0;i<=p;i++)
{
a=a*x;
}
return a;
}
Here is working code:
#include<iostream>
using namespace std;
int pow(int, int);
int main()
{
int x,p,ans;
cout<<"Enter a number";
cin>>x;
cout<<"Enter the power of the number";
cin>>p;
ans=pow(x, p);
cout<<ans;
return 0;
}
int pow(int x, int p)
{
int a=1,i;
for(i=0;i<=p;i++)
{
a=a*x;
}
return a;
}
Ideone
You have to pass the local variables into the function instead of defining new ones with the same name. What you are doing should give you warnings about unused variables (x and p in main) and it also invokes undefined behavior in pow because of ininitialized reads of the variables defined there.
Also your function was wrong. You were just multiplying 1 with a value a bunch of times, which stays 1 forever.
Your function must have the parameters names specified (not just the types):
int pow(int) -> int pow(int b, int p)
You iterate once more than necessary:
for (i = 0; i <= p; i++) -> for (i = 0; i < p; i++)
You can shorten some arithmetic operations:
a=a*x -> a *= x;
The final function:
int pow(int b, int p)
{
int a = 1, i;
for (i = 0; i < p; i++)
a *= b;
return a;
}
You call it by passing the variables precedently declared:
pow(x, p)
So your final code be like:
#include <iostream>
int pow(int b, int p)
{
int a = 1, i;
for (i = 0; i < p; i++)
a *= b;
return a;
}
int main()
{
int x, p, ans;
std::cin >> x >> p;
ans = pow(x, p);
std::cout << ans << std::endl;
return 0;
}
so I have this simple program here and my instructor is asking for the following edits, and I can't see it:
Sorry, you are missing the point. Since all of your functions are
using indexing from 1 to n, where n can be 100, you never use the
element with index zero of the array. That means that the maximum
number of values you will use in the array is 99. If the user really
wants to enter 100 values, your solution then won’t work.
Can you fix your solution again?
#include <iostream>
using namespace std;
int readnums(int v[]);
void findmaxmin(int v[],int n,int &mi,int &ma);
int findmidsum(int v[],int n,int mi,int ma);
int main()
{
const int ARRAYVALUE=100;
int v[ARRAYVALUE];
int n=readnums(v),mi,ma;
findmaxmin(v,n,mi,ma);
cout<<"Max= "<<ma<<endl;
cout<<"Min= "<<mi<<endl;
cout<<"Middle sum= "<<findmidsum(v,n,mi,ma)<<endl;
}
int readnums(int v[])
{
cout<<"How many numbers to enter: ";
int n=0;
cin>>n;
for(int a=1;a<=n;a++)
{
cout<<"Enter no. "<<a<<": ";
cin>>v[a];
}
return n;
}
void findmaxmin(int v[],int n,int &mi,int &ma)
{
ma=v[1];
mi=v[1];
for(int a=1;a<=n;a++)
{
if(mi>v[a])mi=v[a];
if(ma<v[a])ma=v[a];
}
}
int findmidsum(int v[],int n,int mi,int ma)
{
int s=0;
for(int a=1;a<=n;a++)
if(v[a]!=mi && v[a]!=ma)
s+=v[a];
return s;
}
As I mentioned in my comment, you have to iterate from a=0 to a<n rather than from a=1 to a<=n. But you also have to modify the following lines:
ma=v[1];
mi=v[1];
...to be:
ma=v[0];
mi=v[0];
...or else, as you mentioned, the max calculation breaks (because v[1] does not exist when there is only one index in the array).
Here's the complete updated code:
#include <iostream>
using namespace std;
int readnums(int v[]);
void findmaxmin(int v[],int n,int &mi,int &ma);
int findmidsum(int v[],int n,int mi,int ma);
int main()
{
const int ARRAYVALUE=100;
int v[ARRAYVALUE];
int n=readnums(v),mi,ma;
findmaxmin(v,n,mi,ma);
cout<<"Max= "<<ma<<endl;
cout<<"Min= "<<mi<<endl;
cout<<"Middle sum= "<<findmidsum(v,n,mi,ma)<<endl;
}
int readnums(int v[])
{
cout<<"How many numbers to enter: ";
int n=0;
cin>>n;
for(int a=0;a<n;a++)
{
cout<<"Enter no. "<<a+1<<": ";
cin>>v[a];
}
return n;
}
void findmaxmin(int v[],int n,int &mi,int &ma)
{
ma=v[0];
mi=v[0];
for(int a=0;a<n;a++)
{
if(mi>v[a])mi=v[a];
if(ma<v[a])ma=v[a];
}
}
int findmidsum(int v[],int n,int mi,int ma)
{
int s=0;
for(int a=0;a<n;a++) {
if(v[a]!=mi && v[a]!=ma)
s+=v[a];
}
return s;
}
You can test the above code out here: http://cpp.sh/6ogo
Your array contains 100 int, therefore if the entered n is bigger than 99 your program might crash.
int main()
{
const int ARRAYVALUE=101;
int v[ARRAYVALUE];
int n=readnums(v),mi,ma;
findmaxmin(v,n,mi,ma);
cout<<"Max= "<<ma<<endl;
cout<<"Min= "<<mi<<endl;
cout<<"Middle sum= "<<findmidsum(v,n,mi,ma)<<endl;
}
int readnums(int v[])
{
cout<<"How many numbers to enter: ";
int n=0;
Do{
cout << "Enter a number in the range 1 - 100 :" << endl
cin>>n;
}
while (n > 100 || n < 1);
for(int a=1;a<=n;a++)
{
cout<<"Enter no. "<<a<<": ";
cin>>v[a];
}
return n;
}
fixed solution so the user can enter 100 values, your array contains 101 ints.
# include <iostream>
using namespace std;
class mm
{
private:
int k[1000];
int n;
int i;
int a;
int b;
int f;
public:
mm ()
{
a=0;
b=1;
f=0;
i=0;
for(int i=0; i<n;i++)
k[i]=0;
};
~mm()
{
}
void fib(int n)
{
for (int i=0;i<n;i++)
{
if (i<=1)
f=i;
else
{
f=a+b;
a=b;
b=f;
}
k[i]=f;
}
for (int j=0;j<n;j++)
cout<<k[j]<<" ";
}
int se (int n, int i)
{
if (n==1)
return 1;
else
return 1/k[i] + se (n-1, i+1);
}
};
int main()
{
int n;
cout<<"Enter n:";
cin>>n;
mm p;
cout<<"fib: "<<endl;
p.fib(n);
cout<<endl;
cout<<"se: ";
cout<<p.se(n,0);
return 0;
}
Recursion function from main is not responding. Maybe the array k[i] is not working, but I cant find the reason. Can anyone help me?
k[0] is set to 0. When you then call se(n,0) in main, it computes 1/k[0] + se(n-1,1) which is a division by zero.
I am stuck on this question getting WA.
I have seen many bottom up implementations of this question. My top-down implementation is not working with memoization and is working fine without it. How can i correct it ??
#include<cstdio>
#include<cstring>
#include<iostream>
#define INF 0x7FFFFFFF
using namespace std;
int o,n,num,ox[2000],nt[2000],wt[2000];
int dp[2000][2000];
int dive(int index,int oxygen,int nitrogen,int weight) {
if(dp[oxygen][nitrogen]!=-1) return dp[oxygen][nitrogen];
int &ret=dp[oxygen][nitrogen];
if(oxygen>=o&&nitrogen>=n) {
ret=weight;
return ret;
}
if(index==num) {
ret=INF;
return ret;
}
ret= min(dive(index+1,oxygen+ox[index],nitrogen+nt[index],weight+wt[index]),dive(index+1,oxygen,nitrogen,weight));
return ret;
}
main() {
int c;
scanf("%d",&c);
while(c--) {
memset(dp,-1,sizeof(dp));
scanf("%d%d",&o,&n);
scanf("%d",&num);
for(int i=0;i<num;++i) {
scanf("%d%d%d",&ox[i],&nt[i],&wt[i]);
}
printf("%d\n",dive(0,0,0,0));
}
return 0;
}
Try this test:
1
21 79
5
1 1 800
1 1 800
1 1 800
1 1 800
17 75 800
It seems to me the correct answer for it should be 800 * 5 = 4000 right? Your program outputs something else.
This gives wrong answer too :( could someone check pls
#include<iostream>
using namespace std;
int swap( int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
int min(int a, int b)
{
if(a<b)return a;else return b;
}
int max(int a,int b)
{
if(a>b)return a;else return b;
}
int minweight(int ans[][22][80],int arr[][3],int n,int oxy,int nit)
{
int prev=0,curr=1;
for(int i=1;i<=n;i++)
{
for(int j=0;j<=21;j++)
for(int k=0;k<=79;k++)
ans[curr][j][k]=min( ans[prev][j][k], arr[i][2]+ans[prev][max(0,j-arr[i][0])][max(0,k-arr[i][1])]);
swap(curr,prev);
}
return ans[n&1][oxy][nit];
}
int main()
{
int cases;
cin>>cases;
while(cases--)
{
int oxy,nit;
cin>>oxy>>nit;
int n;
cin>>n;
int arr[n+1][3];
for(int i=1;i<=n;i++)
{
cin>>arr[i][0]>>arr[i][1]>>arr[i][2];
}
int ans[2][22][80];
for(int j=0;j<oxy+1;j++)
for(int k=0;k<nit+1;k++)
ans[0][j][k]=9999;
ans[0][0][0]=0;
cout<<minweight(ans,arr,n,oxy,nit);
}
}