0

Im programming in c VS 2013

im trying to multiplicate the a[i] num by 10 raised by (max -1 -i). when max is the size of array, and i is initialed for (max -1). im using a for loop, i--.

im using the pow() function, and therefor had to define i as a float, or a double. Maybe max too, but it doesnt give me an error.

int sum = 0;
float j = 0, i;
int max;

printf("Enter number of digits: ");
scanf_s("%d", &max);

int *a;
a = (int *)malloc(max * sizeof(* a));

for (i = 0; i < max; i++)
{
    printf("\nEnter the %d digt: ", i + 1);
1   scanf_s("%d", &a[i]);
}

for (i = max - 1; i >= 0 ; i--)
{
2   sum = sum + a[i]*pow(10, max -1 -i);
    j++;
}

When trying to compile, it gives me an error at lines 1 and 2.. pointing on [i] and saying that expression must have integral or unscoped enum type

the (max - 1 -i) b.t.w is instead of an extra variable.. im trying to minimize them

help?

Daniel
  • 198
  • 2
  • 14

2 Answers2

3

Exactly as the error message says. i must have an integral type such as int. You can't use a float as an array index.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • ok. so how can i solve this problem as described?.. regards to the pow() – Daniel Sep 08 '15 at 17:55
  • i meant, how can i use the pow() func without giving him j.. but giving him (max - 1 - i) – Daniel Sep 08 '15 at 19:20
  • I don't understand your question. You're already doing it with `pow(10, max -1 -i)`. Is there something wrong with that? – John Kugelman Sep 08 '15 at 19:28
  • Quit being rude. That call is fine. `int`s are automatically promoted to `float` or `double` as needed. Do you have `#include ` at the top of your source file? – John Kugelman Sep 08 '15 at 21:49
  • for sure.. otherwise i couldnt even use that function.. any way, i cant run the prog like this.. gives an error.. did u check it urself? – Daniel Sep 08 '15 at 22:08
  • Check [the link @n.m. posted](http://ideone.com/ztVbFi). That shows the program working. – John Kugelman Sep 08 '15 at 23:31
  • 1
    Also **what error does it give?** Stop asking us what we're seeing, or telling us to go check our error menu, whatever that is. **Tell us what you're getting**, because we're not getting what you're getting. – John Kugelman Sep 08 '15 at 23:34
2

[too long for a comment]

If you have (more or less) complex expression in one line which does not compile, distrubute it over several lines to give the compiler a chance to point you what it does not like:

For example replace

sum = sum + a[i]*pow(10, max -1 -i);

by

sum = 
  sum + 
    a[i] * 
      pow(
        10, 
        max 
          -1 
          -i);

After have fixed the bug, remove those masive new-lines.

alk
  • 69,737
  • 10
  • 105
  • 255