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;
}
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.