Home Work: Create an array of 20 integers [-50;50]

Hello guys. I need ur help with my homework.

I need to create array of 20 integer type numbers between -50 and 50.
Then:

1. Count positive numbers of array (Count 0 as positive)

2. The sum of array located after the last zero element (if appropriate
element to output 0)

3. Transforming the array so as to be arranged at the beginning of all elements is less than ones, but then all the other elements

Please help guys, hope for soon answers! :)
Last edited on
hope for soon answers!

Well, you better start coding. Noone is going to stop what they are doing just to give you answers. We have a life, just like you do. Once you do your work, then post it and we may be able to help you by guiding you to the right direction.

Take a look at this, it could help you out with what you are asking for:

http://www.cplusplus.com/forum/articles/40071/

hope for soon code!
I have made an array of 20 integer numbers.


1
2
3
4
5
6
7
8
9
10
int main()
{
          int array[20], i;
          for(i = 0; i < 20; i++)
          {
                  array[i] = (rand() % 101)-50;
                  printf("%4d\n", array[i]);
       }
    return 0;
}


I ran prog arround 10 times and got no 0 as one of random number, why?
Last edited on
You also didn't likely get a '23' as one of the random numbers.
Also, don't forget to randomize.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int array[20];
    int i, npositive;

    srand(time(NULL));

    // Fill my array with random numbers in [-50,50]
    for(i = 0; i < 20; i++)
    {
        array[i] = (rand() % 101)-50;
    }

    // Show the user the numbers
    puts( "The generated array:" );
    display( array, 50 );

    // Count the number of positive elements
    npositive = 0;
    for (i = 0; i < 20; i++)
    {
        ...
    }
    printf( "The number of positive elements is %d\n", npositive );


Now, I do not understand requirement 2. What is the "sum of array located after the last zero element"?

Does that mean the sum of all elements following the last zero element?
    1 2 0 3 4 0 5 6 --> 11

Does that mean the sum of all elements before the last zero element?
    1 2 0 3 4 0 5 6 --> 10

Keep in mind that there may not be a zero element. What are you supposed to output?
    1 2 3 4 5 6 --> 21     1 2 3 4 5 6 --> 0
(Both answers are correct. Ask your professor for clarification on which one he wants.)


To transform the array you are doing what is called partitioning it. Any element less than one must come before any element not less than one:
    1 -2 3 -4 5 0 -7 --> -2 -4 0 -7   1 3 5
It does not matter what order the elements are except that the ones less than one come before the ones that are not less than one. The following are all correct:
    -4 -2 -7 0   5 1 3
    -7 -4 -2 0   1 3 5
    0 -2 -4 -7   5 3 1

etc.
You can do this two different ways.

1. using an additional array:
    copy all the elements less than one into the new array
    copy all the elements not less than one into the new array
    copy new array to overwrite the original array

2. in-place
    for each element less than one, swap it with element array[20-num_swaps]

I recommend you try method 1.

Good luck!
First of all thanks for answer.

Yes, it mean sum of all elements following the last zero element!
1 2 0 3 4 0 5 6 --> 11
And if there's no 0 then output 0.
1 2 3 4 5 6 --> 0

And why i got syntax error trying to run ur prog?
display( array, 50 );
Is it line before?
puts( "The generated array:" );
Last edited on
It is because my example did not have a function named "display" -- you'd have to write it yourself.

1
2
3
4
void display( int* array, int size )
{
  /* a function which displays 'size' elements of the 'array' */
}

If you haven't learned functions yet (you should have) then you'd have to write the loop to display the array everywhere you want to display the array to the user. (With the function, you'd only need to call it.)


Finding the last zero element is a variation on the 'find the maximum/minimum element' problem. You can actually compute the sum of all elements following the last zero in a single pass: count backwards through the array, summing elements as you go. When you find a zero, break out of the loop and use the current sum. If you get through the entire array without finding a zero, set the sum to zero. (Hint: you'll need a variable to keep the sum as you go, and a variable to keep track of whether or not a zero was found. If you stick this all in a function, you don't actually need that second variable.)


Hope this helps.
Topic archived. No new replies allowed.