mercredi 3 juin 2020

Multiplying strings/arrays and global arrays

so I was doing this code in which you scan a number then that number pairs of numerical strings example

5

5 5

6 6

7 7

8 8

and the program should then output

25 36 49 64 81

which is the first number *the second number

but mine outputs

25

61 //25+36

110 //110+49

174 //110+64

the reason is that in my code I keep each multiplication in an array called arr2 which I declared globally and since I don't reset all values back to 0 it keeps adding up but the problem is don't know how to reset all of its values back to 0 since when I tried it would not let me

code in which I did not try to reset the array which works the way I already mentioned

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<long long> vi;
typedef pair<long long,long long> pi;
typedef vector<pi> vpi;

#define FOR(i, a, b) for(ll i=ll(a); i<ll(b); i++)
#define ROF(i, a, b) for(ll i=ll(a); i>=ll(b); i--)
#define f first
#define s second
#define pb emplace_back
#define mp make_pair
#define SQ(a) (a)*(a)
#define all(a) (a).begin(), (a).end()
int arr[9999];

int multiply(string a,string b,int n){
  int r=0,j=0;
  int x=int(b[0]-48);
  for(int i=n-1;i>=0;i--){
int mult=0,y=int(a[i]-48);
mult=x*y+r;

arr[j]=mult%10;
r=(mult-mult%10)/10;
j++;
  }
  if(r!=0){arr[j]=r;return n+1;}else{return n;}
}
int arr2[9999]={0},u=0;//here I declared it
int suma(int w,int f){

int b[w];
 int r=0;
 int s=0,j=0;
w=w+f;
for(int i=f;i<w;i++){
s=arr2[i]+arr[i-f]+r;
arr2[i]=s%10;
r=(s-s%10)/10;
j++;
}
if(r!=0){
if(w>=u){arr2[j]=r;j++;}
else{
while(r!=0){
  s=arr2[j]+arr[j-f]+r;
arr2[j]=s%10;
r=(s-s%10)/10;
j++;
}

}
}
int u=max({w,u,j});
return u;
}

int main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int as;
cin>>as;
for(int cfg=0;cfg<as;cfg++){
int w,k=0,l=0;
string a,b,c;
cin>>a>>b;
reverse(b.begin(),b.end());
for(int i=0;i<b.length();i++){
  c=b[i];
  w=multiply(a,c,a.length());
k=suma(w,i);
l=max(k,l);
}

int narr[l];
narr[l]={0};
copy(arr2+0,arr2+l,narr);
 int n = sizeof(narr) / sizeof(narr[0]); 
reverse(narr, narr + n); 
int qw=0;
for(int i=0;i<l;i++){
if(narr[i]!=0){qw=1;}
  if(qw==1){
    cout<<narr[i];
  }

}
cout<<endl;
}
 return 0;
}

Code in which I tried but gives me an error:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<long long> vi;
typedef pair<long long,long long> pi;
typedef vector<pi> vpi;

#define FOR(i, a, b) for(ll i=ll(a); i<ll(b); i++)
#define ROF(i, a, b) for(ll i=ll(a); i>=ll(b); i--)
#define f first
#define s second
#define pb emplace_back
#define mp make_pair
#define SQ(a) (a)*(a)
#define all(a) (a).begin(), (a).end()
int arr[9999];

int multiply(string a,string b,int n){
  int r=0,j=0;
  int x=int(b[0]-48);
  for(int i=n-1;i>=0;i--){
int mult=0,y=int(a[i]-48);
mult=x*y+r;

arr[j]=mult%10;
r=(mult-mult%10)/10;
j++;
  }
  if(r!=0){arr[j]=r;return n+1;}else{return n;}
}
int arr2[9999]={0},u=0;//here I declared it
int suma(int w,int f){

int b[w];
 int r=0;
 int s=0,j=0;
w=w+f;
for(int i=f;i<w;i++){
s=arr2[i]+arr[i-f]+r;
arr2[i]=s%10;
r=(s-s%10)/10;
j++;
}
if(r!=0){
if(w>=u){arr2[j]=r;j++;}
else{
while(r!=0){
  s=arr2[j]+arr[j-f]+r;
arr2[j]=s%10;
r=(s-s%10)/10;
j++;
}

}
}
int u=max({w,u,j});
return u;
}

int main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int as;
cin>>as;
for(int cfg=0;cfg<as;cfg++){
 arr2[9999]={0};//here I try to set all the values back to 0
int w,k=0,l=0;
string a,b,c;
cin>>a>>b;
reverse(b.begin(),b.end());
for(int i=0;i<b.length();i++){
  c=b[i];
  w=multiply(a,c,a.length());
k=suma(w,i);
l=max(k,l);
}

int narr[l];
narr[l]={0};
copy(arr2+0,arr2+l,narr);
 int n = sizeof(narr) / sizeof(narr[0]); 
reverse(narr, narr + n); 
int qw=0;
for(int i=0;i<l;i++){
if(narr[i]!=0){qw=1;}
  if(qw==1){
    cout<<narr[i];
  }

}
cout<<endl;
}
 return 0;
}

So the question would be if is there any way to reset all the values to 0 or any other way to solve the problem if you can you could submit a code that works if not just tell me how to fix it,thank you

Aucun commentaire:

Enregistrer un commentaire