4

I am a beginner to C (learning off free resources), I am trying to write a simple code to add a set of numbers inputted in by a user into a set of arrays....what am I doing wrong?

int numbers;
    int number=1;
    int counter = 0;
    int pot[100];
    int calculate;
    int result;

printf("how many numbers do you want to calculate?:"); //asking user how many different integers would be calculated

scanf("%d",&numbers);

while (counter < numbers)

{
    printf("input number %d\n", number);//asking user to input figures to be calculated
    scanf("%d",&pot[0]+1); //user would input values into spaces in the array

    counter++;
    number++;

}


printf("Please press 1 for addition and 2 for multiplication");
scanf("%d",&calculate);

switch (calculate)

{
case 1: result = pot[0]+ pot[0]+1;//this is supposed to add all the individual values within the arrays

printf("the result is %d", result);
break;

case  2: result = pot[0]* pot[0]+1;//this is supposed to multiply all the individual values within the arrays
break;
}



    return 0;
5
  • 3
    What's the input, expected output, and actual output? Commented Jul 8, 2016 at 14:22
  • 1
    What is &pot[0]+1? Commented Jul 8, 2016 at 14:23
  • Apologies, I don't understand the question (newbie problems)! Commented Jul 8, 2016 at 14:25
  • pot[0]+1 is my attempt to tell C to allocate the user inputted value to the next available space in the array...assuming the first space is pot[0], therefore the next is pot[1] etc. Commented Jul 8, 2016 at 14:26
  • 1
    (1) check for the numbers to be <100. (2) check return of scanf : while(counter < numbers) { if(scanf("%d", &(pot[counter])) != 1) return 1; counter++; }, just to begin with. Please note that the variable counter is your array's subscription index. Commented Jul 8, 2016 at 14:30

4 Answers 4

2
int numbers;
int number=1;
// int counter = 0; instead used `i`
int pot[100];
int calculate;
int result;
int i = 0;

printf("how many numbers do you want to calculate?:"); //asking user how many different integers would be calculated

scanf("%d",&numbers);

printf("input %d numbers \n", numbers);//asking user to input figures to be calculated

for(i = 0; i < numbers; i++)
{
    printf("input number %d \n", number);
    scanf("%d", &pot[i]);   //user would input values into spaces in the array
    number++;
}

//scanf("%d",&pot[0]+1);     /* this line is wrong*/
//counter++; 
//number++;     


printf("Please press 1 for addition and 2 for multiplication");
scanf("%d",&calculate);

switch (calculate)

{
case 1: result = 0;//this is supposed to add all the individual values within the arrays
        for(i = 0;i < numbers; i++)
        {
             result = result + pot[i];
        }

        printf("the result is %d", result);
        break;

case  2: result = 1;//this is supposed to multiply all the individual values within the arrays
         for(i = 0;i < numbers; i++)
         {
             result = result * pot[i];
         }
         printf("the result is %d", result);
         break;
}



return 0;

This code should work properly for your program.

Sign up to request clarification or add additional context in comments.

4 Comments

don't increment numbers in the loop!
@Jean-BaptisteYunès numbers and number both are different variables. And I think Initializing result won't do any good as 1 operation is addition and another is multiplication. So, we have to change value of result accordingly.
then messages are wrong: "input 1 numbers", "input 2 numbers", "input 3 numbers", etc. Never use two variables for the same thing (i and number are doublons). printf("input number %d: ",i+1); will do the trick.
when I run the program, I get this error: subscripted value is not an array, pointer or vector
1

I'm not sure exactly what you are trying to do, so this is my guess:

int sum(const int *arr, size_t n)
{
    int ret;

    for (ret = 0; --n >= 0; ret += arr[n])
        ;

    return ret;
}
long mpy(const int *arr, size_t n)
{
    long ret;

    for (ret = 1; --n >= 0; ret *= arr[n])
        ;

    return ret;

}
// remove trailing newline
void remTrailingNl(char *buf)
{
    if (buf[strlen(buf) - 1] == '\n')
        buf[strlen(buf) - 1] = '\0';
}

int main()
{
    int *arr, choice, tmp;
    char line[80], *endp;
    size_t n;

    printf("How many numbers are in the array?");
    fgets(line, 80, stdin);
    remTrailingNl(line);
    n = strtol(line, &endp, 10);
    // **endp will be the null character if conversion was successful
    if (**endp == '\0')
        if ((arr = malloc(n * sizeof(int))) == NULL) {
            perror("malloc");
            return 1;
        }

    printf("Begin entering your numbers, each number to a line\n");

    // walking backwards, populate the array
    while (n-- >= 0) {
        fgets(line, 80, stdin);
        remTrailingNl(line);
        tmp = strtol(line, &endp, 10);
        if (**endp == '\0')
            arr[n] = tmp;
        else {
            fprintf(stderr, "Invalid number at position %d\n", n + 1);
            return 1;
        }
    }

    printf("Enter 1 for addition, and 2 for multiplication\n");
    fgets(line, 80, stdin);
    remTrailingNl(line);
    choice = strtol(line, &endp, 10);
    if (**endp == '\0' && choice == 1 || choice == 2) {
        switch (choice) {
            case 1: // add
                printf("Sum is %d\n", sum(arr, n));
                break;
            case 2:
                printf("Product is %ld\n", mpy(arr, n));
                break;
        }
    } else {
        fprintf(stderr, "Invalid choice\n");
        return 1;
    }
    // heap memory must be freed
    free(arr);
    // return 0 on success
    return 0;
}

We use long for mpy, because multiplication can give very large answers. Also, don't use scanf, because it is very tricky to get right, although it looks very innocent. strtol is better, because it tells more details as to why the number was invalid. Read the man pages here. We call remTrailingNl right after fgets is called, because fgets always puts a trailing newline in the buffer.

Comments

1

If you want a simple code just to add numbers in array, where the numbers are given by user, you could just follow with this one.

int i, num, ans=0, pot[100];
printf("Give the number of elements:");
scanf("%d",&num);
printf("Enter numbers");
for(i=0;i<num;i++) {
  scanf("%d",&pot[i]);
  ans = ans + pot[i];
}
printf("Sum is %d", ans);
}

This way the addition is directly shown instead of scanning it first and again having to put in loop and add it again, one by one.

( And I am also a C learner, correct me if I am wrong anywhere. ) Hope this simplifies it.

Comments

0
So, finally got a working program!!

int pot[100];
int numbers;
int count;
int number = 1;
int calculate;
int result;


printf("how many numbers do you want to calculate:");
scanf("%d", &numbers);

for(count=0;count<numbers;count++)

{
printf("enter number %d:\n", number);
scanf("%d",&pot[count]);
number++;

}

printf("Please press 1 for addition and 2 for multiplication");
scanf("%d",&calculate);

switch (calculate)

{
case 1: result = 0;

        for(count = 0;count < numbers; count++)
        {
             result = result + pot[count];
        }

        printf("the result is %d", result);
        break;

case  2: result = 1;

         for(count = 0;count< numbers; count++)
         {
             result = result * pot[count];
         }
         printf("the result is %d", result);
         break;
}

return 0;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.