0

I stuck with a big assignment problem which has this snippet of code

int t;
scanf("%d",&t);
int n[t],p[t];
for(int i=0;i<t;i++)
{
    scanf("%d %d",&n[i],&p[i]);
}
 for( int j=0;j<t;j++)
{

    int k=0;
    int arr[n[j]];
    arr[n[j]]={0};

    for(int i=k;i<n[j];i++)
    {

        arr[i]=arr[i]+1;
        k++;

    }

} 

but in the line arr[n[j]]={0}; has an error("expected expression before '{' token") can you tell me how to declare all zeroes in this array

Sai Kadali
  • 13
  • 4
  • What's it supposed to do? `arr` is a local array and it doesn't seem to be used in any meaningful way and it'll disappear after the loop. May be, you can describe the problem you're trying to solve? – P.P Apr 14 '20 at 11:07
  • `memset(arr, 0, n[j] * sizeof arr[0])` – Support Ukraine Apr 14 '20 at 11:07
  • 1
    Hmm... the proposed duplicate do have the solution but it's not the same problem. This is not initialization - this is an assignment. Not sure it's a good duplicate but again - the solution is in the dup. – Support Ukraine Apr 14 '20 at 11:10
  • @4386427 Yeah, an out of bounds assignment. This question would benefit from a clear exposition of what the code is supposed to do. – Bob__ Apr 14 '20 at 11:11
  • @Bob__ I didn't look at the rest of the code - I only looked at the part OP asked about. Well - true - the code seems a bit strange ... `k++;` is useless as `k` is never used. And given that `arr` is starting out as all-zero, the loop will just set all elements to 1. So the body of the loop could be `arr[i]=1` and then there is no need to set the element to zero in the start. Stramge. – Support Ukraine Apr 14 '20 at 11:18

0 Answers0