1

The question: Given integer values of x and y generate 20 random values within [x, y] interval and put them in an array. Print the array. Determine the index of the largest even negative number in it (ranging from 0 to 19). Account for the possibility that there might be no such number in the array. Calculate the sum of all positive odd numbers. You are only allowed to go through the array values once. You should define separate functions for generating random numbers and checking even and odd numbers.

What I have written:

    #include <stdio.h>

int random(int min, int max, int arr[]){
    int i, j = 1;
    for(i = 0; i<20; i++){
        arr[i] = min + rand() % (max+1 - min);}
    for(i = 0; i<20; i++){
        printf("%d. %d\n", j, arr[i]);
        j++;}
    return arr[20];}

int evenodd(int arr[], int size){
    int i, holder, sum, index = 1;
    holder = arr[i];
    for(i = 0; i<20; i++){
        if (holder>0){
            if (holder%2 == 0){
                sum = sum;}
            else{
                sum += i;}}
        else{
            sum = sum;}}
    printf("\nThe sum of all odd positive numbers is %d.", sum);
    for(i = 1; i<20; i++){
        int top = arr[0];
        if (arr[i]<0){
            if(arr[i]<top){
                top = arr[i];
                index = i;}
            else{
                top = top;}}
        else{
            top = top;}}
    printf("\nThe index of the largest negative number is %d", index);}

int main()
{
    int min, max;
    printf("Please input the minimum desired random number: ");
    scanf("%d", &min);
    printf("\nPlease input the maximum desired random number: ");
    scanf("%d", &max);
    int a[20];
    random(min, max, a);
    evenodd(a, 20);
}

It prints out fine, but the sum of positive odd number is wrong, as well as the index of the largest negative number.

Jack Eaton
  • 13
  • 4

3 Answers3

1

In your loop for(i = 1; i<20; i++) when finding negative numbers, you begin the loop by resetting the value of top to be arr[0], and thus you will always be doing your comparisons against the first value of the array. Perhaps you want to move that initialization out of the loop.

  • I love the OP's (repeated) `else { top = top; }` and `sum = sum;` There is a lot of redundancy in the code which makes it hard to read. – Weather Vane Mar 31 '17 at 20:07
1

I see several issues with your code (especially with respect to your problem statement), but the summation procedure is not working because you are initializing variable "holder" only once, that too, before the for loop. Also, you are using an uninitialized varaible "i" as an index for the array arr. A lot can go wrong here!

By the way, Please use proper indentation to make your code more readable.

0

Four things 1) You did not initialize your sum to zero for odd positive numbers. So you should do that. 2) In calculating the sum you should do sum+=sum[i] and not sum+=i;

3) You initialized your index to 1 and not 0 for index of the largest negative number. If the first element is your smallest negative your answer should be zero. Right now for both first and second element you will give answer as 1. If you want a range from 1 to 20 you can output i+1 in the prints. I think you also have to do if(arr[i]>top) to find the largest negative integer assuming -10 is larger than -20 which is usually the case

4) Your implementation in three still wont account for the fact that no such number exists. What I recommend doing is initialize your top to largest negative integer. Range of values in C Int and Long 32 - 64 bits . And you can set your index to -1. Compare this to the entire array and if your index is -1 in the end. Then you print that no such element exists. You also have to check if the integer you are comparing is even. You can do the summing and the even negative integer in one loop as the two things are independent of each other

Community
  • 1
  • 1
Nirvedh Meshram
  • 439
  • 6
  • 13