0

I am giving a 1000 digit number as input, and using this program to find max product of 5 consecutive integers. The array a is used to hold the sequence with highest product.I am getting an unexpected answer(I suspect the problem to be in conversion from char to int)

#include <stdio.h>
int main(void) 
    {
    int a[5],c=0,b,i=1;
    char *num[1000];
    scanf("%s",&num);
    while(i<5)
    {
        a[i]=num[i]-'0';
        i++;
    }
    while(i<1000)
    {
        b=(char)num[i]-'0';
        if(a[c]<b)
        {
            a[c]=b;
            c=(c+1)%5;
        }i++;
    }
    printf("%d",a[0]*a[1]*a[2]*a[3]*a[4]);
    return 0;
}
Ali
  • 56,466
  • 29
  • 168
  • 265
Nitin Labhishetty
  • 1,290
  • 2
  • 21
  • 41
  • 1
    Besides fixing the type, if you want to store 1000 characters, you need to allocate extra memory for a terminating null at the end, so make `num` at least 1001. – woolstar Jan 21 '14 at 18:41

1 Answers1

2

Your code to allocate and read the string is wrong. You are allocating an array of 1000 pointers. You meant to write:

char num[1000];
scanf("%s", num);

The rest of your code is full of errors too. You meant to initialise i to 0. And you need to set it back to 0 before the second loop. And your second loop runs to 1000 and so accesses uninitialized elements of num.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490