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.