-2

Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra to the total extra credit received. Full credit is 100, so anything over 100 is extra credit. Ex: If testGrades = {101, 83, 107, 90}, then sumExtra = 8, because 1 + 0 + 7 + 0 is 8.

Please code using C language

#include <stdio.h>

int main(void) {
   const int NUM_VALS = 4;
   int testGrades[NUM_VALS];
   int i;
   int sumExtra = -9999; // Assign sumExtra with 0 before your for loop

   for (i = 0; i < NUM_VALS; ++i) {
      scanf("%d", &(testGrades[i]));
   }

   /* Your solution goes here  */

   printf("sumExtra: %d\n", sumExtra);
   return 0;
}
Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
  • What's your question? – Andrey Tyukin Oct 13 '22 at 21:09
  • FYI, you can solve this without an array. Calculate `sumExtra` as you read in the values. – 001 Oct 13 '22 at 21:15
  • @JohnnyMopp the array is already given, it's not part of the task. – Andrey Tyukin Oct 13 '22 at 21:17
  • Then OP will need a 2nd loop. It should check each value of `testGrades` and `if` the value is `> 100`, subtract 100 and add the result to `sumExtra`. You will need to set `sumExtra = 0;` before that. – 001 Oct 13 '22 at 21:21
  • Most would use `#define NUM_VALS 4` instead of the `const int` variable. See https://stackoverflow.com/questions/1674032/static-const-vs-define-vs-enum – Allan Wind Oct 13 '22 at 21:22
  • `int sumExtra = -9999; // Assign sumExtra with 0...` Seems, Sara, that you haven't even done THAT in the code... Are you willing to share 100% of your income in exchange for others doing 100% of the work? If you have troubles in the "paddling pool", how do you expect to survive and make headway in the open ocean? – Fe2O3 Oct 13 '22 at 21:29
  • _"Are you willing to share 100% of your income in exchange for others doing 100% of the work?"_ - "income"? Do you want to get the grade for this Programming 101 homework assignment? Do you insist on getting the original on paper, or would you take it as an NFT? @Fe2O3 – Andrey Tyukin Oct 13 '22 at 21:33
  • @AndreyTyukin It is the _rare_ individual who pays massive tuitions and invests hours in learning to program without an eye on a future career in this (often) lucrative industry... The princess used Rumpelstiltskin to weave straw into gold while she married the king and sipped martinis on the verandah with the upper echelon... Sara seems to think SO is populated with Rumpelstiltskin clones... – Fe2O3 Oct 13 '22 at 21:37
  • 1
    @AndreyTyukin ... at least I didn't say I would write the code in exchange for her first-born... – Fe2O3 Oct 13 '22 at 21:44
  • 1
    @Fe2O3 Ok, I've just spent the past ten minutes or so deciphering [this very nice print](https://www.deutschestextarchiv.de/book/view/grimm_maerchen01_1812/?hl=Rumpel%C5%BFtilzchen&p=287) from 1812; Weird trippy stuff, I wonder what they were smoking back then... You've totally made my day, thank you Sir (or Ma'am... or Rust... or whatever). – Andrey Tyukin Oct 13 '22 at 22:01
  • 1
    @AndreyTyukin OMG!!! Just noticed the OP's surname!!! Go back to that 1812 original and read how the girl's family (father) generated the family's income... Spooky!!! `:-)` – Fe2O3 Oct 13 '22 at 22:13
  • 1
    Oh, indeed, "miller" / "Müller"... Ó_o – Andrey Tyukin Oct 13 '22 at 22:15
  • 1
    Welcome to Stack Overflow. Please read the [About](http://stackoverflow.com/tour) page soon and also visit the links describing [How to Ask a Question](http://stackoverflow.com/questions/how-to-ask) and [How to create a Minimal Complete Reproducable Example](https://stackoverflow.com/help/minimal-reproducible-example). Providing the necessary details, including your MCRE, compiler warnings and associated errors, and sample data if any, will allow everyone here to help you with your question. – David C. Rankin Oct 13 '22 at 22:45
  • When asking about homework you should **(1)** Make a good faith attempt to solve the problem yourself first (include your code in your question). **(2)** Ask about specific problems with your existing implementation (including any errors you are receiving). **(3)** Admit that the question is homework. **(4)** Be aware of your school policy. **(5)** Never use code you don't understand. See: [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822/3422102) (from the context, it is amply apparent this is a homework question) – David C. Rankin Oct 13 '22 at 22:46

1 Answers1

2

Write a function:

#include <stdio.h>
                                                               
int calcSumExtra(size_t n, int a[n]) {
    int s = 0;
    for(size_t i = 0; i < n; i++)
        s += (a[i] > 100) * (a[i] - 100);
    return s;
}

int main(void) {
   const int NUM_VALS = 4;
   int testGrades[NUM_VALS];
   int i;
   int sumExtra = -9999; // Assign sumExtra with 0 before your for loop
   for (i = 0; i < NUM_VALS; ++i) {
      scanf("%d", &(testGrades[i]));
   }

   /* Your SOlution goes here */
   sumExtra = calcSumExtra(NUM_VALS, testGrades);

   printf("sumExtra: %d\n", sumExtra);
   return 0;
}
Allan Wind
  • 23,068
  • 5
  • 28
  • 38