This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Mastering Corda: Blockchain for Java Developers and have Jamiel Sheikh on-line!
See this thread for details.
Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Faulty Average Score

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C program.

Problem: Faulty average score output.

My program gives faulty average score output, no matter how I debug it, it is still there, and it has become a pain in the ass lol.



Any help would be greatly appreciated.
Screenshot-2020-12-24-at-6.19.56-PM.png
Average score output
Average score output
 
Rancher
Posts: 287
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eve - please could you post the rest of the code as well so I can compile it?
 
Eve Wein
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Matthews wrote:Hi Eve - please could you post the rest of the code as well so I can compile it?

Sure, John! I have tried sending the complete code, but received a "Post is too long, please shorten it. Alternatively you can buy some Pie from an upper menu to increase a post limit by 10000 characters per pie slice," warning. So I have posted my code to this link https://paste.ubuntu.com/p/7ZVmwnsX22/ feel free to check it out!
 
John Matthews
Rancher
Posts: 287
11
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I just added a printf() in the code where the average score is calculated and played one round - the printf() showed the average being updated correctly.

Then I was about to play my 2nd round - entered 'P' - and the printf() output again; my first round score had been added to allScore again, so it was no longer correct.

Try adding some printfs yourself - it's a simple way to debug code, doesn't need a fancy debugger.
 
John Matthews
Rancher
Posts: 287
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and the output shown in your original post confirms my printfs.

The output shows an (incorrect) average of 127. Well it looks like your first round score was 84, and second round was 87. (84 + 84 + 87) = 255, and (255 / 2) = 127.
 
Eve Wein
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Matthews wrote:Thanks. I just added a printf() in the code where the average score is calculated and played one round - the printf() showed the average being updated correctly.

Then I was about to play my 2nd round - entered 'P' - and the printf() output again; my first round score had been added to allScore again, so it was no longer correct.

Try adding some printfs yourself - it's a simple way to debug code, doesn't need a fancy debugger.

Thank you, may I ask which line of the code are you referring to?
 
John Matthews
Rancher
Posts: 287
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where the average is calculated, the ones you've helpfully marked [b] in your original post.
 
Eve Wein
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Matthews wrote:Where the average is calculated, the ones you've helpfully marked [b] in your original post.


Ah, I got it! Thank you.
 
John Matthews
Rancher
Posts: 287
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suggestions for improving/correcting print_positive_message():

  • (correction) The function argument should be void; if left empty it means unspecified (although it would be correct in C++).
  • The function should be static - it is only used in this file.
  • The words array should be static, otherwise it is created on the stack every time the function executes.
  • The word array should be a (const) one dimensional array of (const) char pointers, not a 2 dimensional array of char. The compiler will store the strings in memory for you, only using as much memory as it needs to.
  • Use the ARRAY_SZ() macro to give the size of the array for the modulo calculation; avoids the use of 'magic numbers' and reduces maintenance overhead.
  • Move the '\n' and '!' into the printf() format string because they are common to all strings.
  •  
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic