BazookaBan
BazookaBan

Reputation: 1

Double arrays in C

Im in the process of learning C and the basis of the class is C primer plus(6th edition). We use Eclipse as an IDE.

For an project we have to create to arrays. One array that takes numbers in a loop and another array that display the cumulative value. So if array 1 has values 1, 5 and 3(out of 10 inputs total) then the resulting input in array 2 should be 9(on the 3th input because of the 3 inputs in array 1).

Im having trouble getting started the right way - anyone here has ideas how I could proceed? So far I have this for starters but forgive me for it it very weak:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 10
void doublearrays (double usernumber);

int main(void)
{
    double usernumbers = 0.0;
    int loop1 = 1;
    while(loop1)
    {
        printf("Type numbers as doubles. \n");
        fflush(stdout);
        loop1 = scanf("%lf", &usernumber);
        if(loop1)
        {
            doublearrays(usernumber);
        }
    }
    return 0;
}

Upvotes: 0

Views: 784

Answers (3)

Jonas Karlsson
Jonas Karlsson

Reputation: 1

The straight forward way of doing this, which would also use two arrays and a loop construct would be to create something like this.. I've changed the doubles to integers. (and i am also ignoring any errors from scanf()).

#include <stdio.h>
#include <stdlib.h>

#define SIZE 10

static void
print_array(int *arr, const char *arr_name)
{
   int i;
   printf("%s  = [");
   for (i = 0; i < SIZE; i++)
      printf("%d%s", arr[i], i < SIZE -1 ? ",":"");
   printf("]\n");
}


int main(int argc, char **argv)
{
   int i;
   int input[SIZE];
   int cumsum[SIZE];

   for (i = 0; i < SIZE; i++)
   {
      int _input;

      printf("Give me numbers!\n");
      fflush(stdout);
      scanf("%d", &_input); /* assuming integer */
      input[i] = _input;
      cumsum[i] = i > 0 ? cumsum[i-1] + _input : _input;
   }

   print_array(input, "input");
   print_array(cumsum, "cumulative");

   return 0;
}

or If you'd like to play around with pointers and have a bit more compact version.. perhaps this could be something to study to help you understand pointers, it does the same thing as my code above

#include <stdio.h>
#include <stdlib.h>

#define SIZE 10
static int data[SIZE*2];

int main(int argc, char *argv[])
{
   int *input_p = &data[0];
   int *cumsum_p = &data[0] + SIZE;

   for (; input_p != &data[0] + SIZE; input_p++, cumsum_p++)
   {
      printf("Give me numbers!\n");
      scanf("%d", input_p);
      *cumsum_p = input_p == &data[0] ? *input_p : *(cumsum_p-1) + *input_p;
   }
}

Upvotes: 0

Serge Ballesta
Serge Ballesta

Reputation: 148965

All the text in a homework assignment shall be read:

For a project we have to create two arrays... 10 inputs total...

Why on earth do not you declare them?... You already have defined SIZE so

double usernumbers[SIZE];
double cumulnumbers[SIZE];

Next do yourself a favour and handle one problem at a time:

One array that takes numbers in a loop...

Ok, so write a loop up to 10 reading floats directly into the array and note how many numbers were received

int n;
for(n=0; n<SIZE; n++) {
    if (scanf("%lf", &usernumbers[n]) != 1) break;
}
// ok we now have n number in the first array

Let us go on

and another array that display the cumulative value.

Ok cumul is initially 0. and is incremented on each value from the first array:

double cumul = 0.;
for(int i=0; i<n; i++) {
    cumul += usernumbers[i];
    cumulnumbers[i] = cumul;
}

Upvotes: 2

4386427
4386427

Reputation: 44284

(your current code isn't what you need... delete it and then...)

anyone here has ideas how I could proceed?

Well the first step would be to actually define some arrays.

double input[SIZE];
double cum[SIZE];

The next step would be a loop to read input.

for (int i = 0; i < SIZE; ++i)
{
    if (scanf("%lf", &input[i]) != 1)
    {
        // Input error - add error handling - or just exit
        exit(1);
    }
}

The next step is to add code for calculating the the cumulative value.

I'll leave that for you as an exercise.

The last step is to print the array which I also will leave to you as an exercise.

Upvotes: 1

Related Questions