i got this question and started and got a bit stuck can u please help. it goes like this
the questions asks us to
Write a c-program to read 10 numbers from the keyboard and store the numbers in an array called numbers After storing the numbers in the array do the following:
a). find and print the sum and average of even and odd numbers in the array.
b). find and print the largest and smallest element in the array. Also print the indesxes of the largest and smallest element in the array.

The following is what i did?? Help please thank you

# include <stdio.h>
void main()
{
      int numbers[10],num_val;
      int count,sum_even,sum_odd;
      float average;
      printf("please enter numbers to be added\n");
      scanf("%d",&num_val[numbers]);
      for(count=0;count<10;count+=1)
      { 
           if(num_val[count]%2==0)
           sum_even=sum_even+num_val[count];
           printf("sum even is =%d",sum_even);
           else if(num_val[count]%2!=0)
           sum_odd=sum_odd+num_val[count];
           printf("sum_odd is =%d",sum_odd);
      }
// this is where i got stuck

help please

Dani AI

Generated

The original snippet has a few separate problems: num_val is an int but is used like an array, input is not read inside a loop, sum_even/sum_odd are never initialized, and output is printed inside the input loop. As noticed, an array such as numbers[10] should be filled inside a for loop; as suggested, use int main(void) instead of void main().

A concise, correct approach:

  • read 10 integers into numbers[] with scanf in a loop;
  • keep sum_even, sum_odd, and counters initialized to 0 so averages avoid divide-by-zero;
  • track min/max and their indices (first occurrence) during the same scan;
  • print results after processing.

Example implementation (reads 10 ints, prints sums, averages, largest/smallest and their indices):

#include <stdio.h>

int main(void) {
    int numbers[10];
    int i, sum_even = 0, sum_odd = 0, cnt_even = 0, cnt_odd = 0;
    int min_idx = 0, max_idx = 0, min_val, max_val;

    puts("Enter 10 integers:");
    for (i = 0; i < 10; ++i) {
        if (scanf("%d", &numbers[i]) != 1) return 1;
    }

    min_val = max_val = numbers[0];

    for (i = 0; i < 10; ++i) {
        if (numbers[i] % 2 == 0) { sum_even += numbers[i]; ++cnt_even; }
        else { sum_odd += numbers[i]; ++cnt_odd; }

        if (numbers[i] > max_val) { max_val = numbers[i]; max_idx = i; }
        if (numbers[i] < min_val) { min_val = numbers[i]; min_idx = i; }
    }

    if (cnt_even) printf("Even: sum=%d avg=%.2f\n", sum_even, (double)sum_even / cnt_even);
    else puts("No even numbers entered.");

    if (cnt_odd)  printf("Odd:  sum=%d avg=%.2f\n", sum_odd,  (double)sum_odd  / cnt_odd);
    else puts("No odd numbers entered.");

    printf("Largest: %d at index %d\n", max_val, max_idx);
    printf("Smallest: %d at index %d\n", min_val, min_idx);

    return 0;
}

Notes and troubleshooting:

  • Indices above are 0-based; add 1 for human-friendly positions.
  • Initialize accumulators before use; uninitialized ints cause undefined behavior.
  • scanf return checks catch invalid input early.
  • For duplicate min/max this reports the first occurrence; change comparison to >=/<= to pick last occurrence.
  • Compile with warnings enabled (e.g., gcc -std=c11 -Wall -Wextra) to catch common mistakes.

Recommended Answers

All 7 Replies

Why are treating num_val like an array?

i am kinda confused with is i was wondering if i should have used count, plus my obective is to add the elements entered

Write a c-program to read 10 numbers from the keyboard and store the numbers in an array called numbers After storing the numbers in the array do the following:

You're in the wrong forum. This the C++ forum, not the C-forum.

You're in the wrong forum. This the C++ forum, not the C-forum.

maybe some misunderstanding on your part ?

void main()

int main() is standard. Use that.

You declared numbers[10] to be an array and num_val as an integer, and then started using num_val as an array ??
I guess you should write the prog again from scratch coz you have got it all wrong here.

Declare an array numbers[10], Use scanf inside a for loop to get 10 numbers entered by the user. Then use two int variables sum_even and sum_odd to get the sum of even and odd numbers.

maybe some misunderstanding on your part ?

Nope, this thread got moved from C++ forum to C

Nope, this thread got moved from C++ forum to C

OH....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.