-1

I have the main code I want to use written out. All I need to do is get the user to input five numbers. Then once they insert the numbers I need them to be inputted into the array so that they can be calculated. I have tried to understand how to successfully get user input and use it, but have been unable to figure it out.

int
main (int argc, char **argv)
    {
    int number1;
    int number2;
    int number3;
    int number4;
    int number5;
    int a[5];
    printf("Enter a number\n");
    scanf ("%d, number1");
    printf("Enter a number\n");
    scanf ("%d, number2");
    printf("Enter a number\n");
    scanf ("%d, number3");
    printf("Enter a number\n");
    scanf ("%d, number4");
    printf("Enter a number\n");
    scanf ("%d, number5");
    a[0] = number1;
    a[1] = number2;
    a[2] = number3;
    a[3] = number4;
    a[4] = number5;
    int mean = (a[0] + a[1] + a[2] + a[3] + a[4]) / 5;
    int difference0 = a[0] - mean;
    int difference1 = a[1] - mean;
    int difference2 = a[2] - mean;
    int difference3 = a[3] - mean;
    int difference4 = a[4] - mean; 
    int variance = ((difference0 * difference0) + (difference1 * difference1) + (difference2 * difference2) + (difference3 * difference3) + (difference4 * difference4)) / 5;
    double sdeviation = sqrt(variance);

    printf("the mean of the array is %d\n",mean);
    printf("the variance of the array is %d\n",variance);
    printf("the standard deviation of the array is %f\n",sdeviation);
    return 0;
    }
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

3 Answers3

2

First of all, you don't need the numberN local variables. You can define the array and read the numbers into it directly.

That said, do not repeat the same code over and over again.

Change the code like

#define ARRSIZE 5

int a[ARRSIZE] = {0};
for (int i = 0; i < ARRSIZE ; i++) {
  printf("Enter a number\n");
  scanf ("%d", &a[i]);
 }

Also, it's always a good practice to check the return value of scanf() function to ensure that the scanning was successful.

Suggestion: Always enable compiler warnings and try to resolve the warnings.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

Just to add to the other answer by @Sourav, there are principles you have to learn as a programmer. One of which is, if you have to repeat any task with minor variations, you're most likely going to use a loop. Also, to quote Sinan Ünür

When you find yourself adding an integer suffix to variable names, think I should have used an array.

If you wish to create 5 int variables and you're just naming each one: number1 ... number 5, then you should just create an array to hold them all. It is a lot easier and enables you to refer to each one dynamically because of how arrays work.

int numbers[5]; //Now you can refer to each one by numbers[X]

...

for (int i = 0; i < 5; ++i) {
    numbers[i] = a[i] - mean;
}

Those are just two very small modifications I would have added to your code so that it is a lot smaller and easier on the eyes. You'll learn this a lot better as you go along.

Community
  • 1
  • 1
Keno
  • 2,018
  • 1
  • 16
  • 26
0

You need & operator before the variable you want to assign the upcoming value. On that way, you are saying that num1 = 4, where for is the user's value.

  • When you declare same type variables, just put then in row separated by comma. You don't need a new line for each one.It makes your code more legible
  • int argc and char **argv params, are not being used in this function and instead are consuming unnecessary ram memory

Try this:

#include <stdio.h>
#include <math.h>

int main()
{

    int num1,num2, num3, num4, num5;

    printf("Enter #1:\n");
    scanf ("%d", &num1);
    printf("Enter #2:\n");
    scanf ("%d", &num2);
    printf("Enter #3:\n");
    scanf ("%d", &num3);
    printf("Enter #4:\n");
    scanf ("%d", &num4);
    printf("Enter #5:\n");
    scanf ("%d", &num5);

    int mean = (num1 + num2 + num3 + num4 + num5) / 5;
    int difference0 = num1 - mean;
    int difference1 = num2 - mean;
    int difference2 = num3 - mean;
    int difference3 = num4 - mean;
    int difference4 = num5 - mean;

    int variance = ((difference0 * difference0) + (difference1 * difference1) + (difference2 * difference2) + (difference3 * difference3) + (difference4 * difference4)) / 5;
    double sdeviation = sqrt(variance);

    printf("the mean of the array is %d\n",mean);
    printf("the variance of the array is %d\n",variance);
    printf("the standard deviation of the array is %f\n",sdeviation);

    return(0); 
}
Sebastian S
  • 807
  • 6
  • 15