K
K
katmandu040917202021-09-06 05:42:55
C++ / C#
katmandu04091720, 2021-09-06 05:42:55

Product of array elements located after the last zero element?

Hello! We need to find the product of the array elements located after the last zero element. I start enumeration from the end of the array, the answer is 0. Please tell me where to dig?

#include <stdio.h>
#include <stdlib.h>
#define N   10
 
int main(int argc, char* argv[])
{
    short str[N] = {0};
    
    
    srand(time(NULL));
  	for(int i=0;i<N;i++)   
    str[i] = rand()%5;
    for(int i = 0; i < N; i++) {
    	
        printf("%2d ",str[i]);
        
  }
  printf("\n");  
  system("PAUSE");
    
  int a = 0;
  for(int i = N - 1; i >= 0; i--)
  {	
    a *= str[i];
  
  }
  
  printf("%2d", a);
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2021-09-06
@katmandu04091720

The cycle you have is not up to the element with zero value, but up to the element with zero index.
Well, you need to initialize with a unit, not zero, as dollar already wrote .

D
dollar, 2021-09-06
@dollar

This is not a sum, but a product , therefore: PS I did not read the whole code, because incorrect indentation makes the code hard to read. A mixture of spaces and tabs indicates that the code was partially copied from somewhere and supplemented / redone for the current task.
int a = 1; // вместо 0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question