Thread: little help with a C exercise

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by whiteflags View Post
    In that case it is literally a normal average: you should make a running total out of the ASCII value of characters you've printed so far, and then divide it by the number of letters you've printed so far.
    Yep. Probably easiest if the for loop runs from 'a' to 'z', inclusive, and either keep a separate counter for the number of letters printed so far or realize that value is the current letter minus 'a' plus 1.

    You could also work this the other way - loop from 0 to 25 to get 26 lines of output. Do the math as if the characters 'a' - 'z' were numbered 0 to 25. Adjust to the actual ASCII values in the printf() by adding 'a' to each value you print.

    Either way is good. Maybe try both just to be sure you understand what's going on.

    However, I would insist that characters are not meant to be numbers in a mathematical sense.
    It is just a different encoding -> 97 == 0x61 == 01100001 == 0141 == ASCII 'a'. All are equally valid representations of the same numerical value. There is some weirdness in C in char vs. signed char vs. unsigned char and so on, but I doubt that will be an issue for this assignment.

  2. #2
    Registered User
    Join Date
    Sep 2013
    Posts
    6
    Quote Originally Posted by KCfromNC View Post
    Yep. Probably easiest if the for loop runs from 'a' to 'z', inclusive, and either keep a separate counter for the number of letters printed so far or realize that value is the current letter minus 'a' plus 1.

    You could also work this the other way - loop from 0 to 25 to get 26 lines of output. Do the math as if the characters 'a' - 'z' were numbered 0 to 25. Adjust to the actual ASCII values in the printf() by adding 'a' to each value you print.

    Either way is good. Maybe try both just to be sure you understand what's going on.



    It is just a different encoding -> 97 == 0x61 == 01100001 == 0141 == ASCII 'a'. All are equally valid representations of the same numerical value. There is some weirdness in C in char vs. signed char vs. unsigned char and so on, but I doubt that will be an issue for this assignment.
    thnx for your suggestion however the main problem i am facing is how to calculate the average as asked in the second columm and then display it at the same time next to the alphabets.
    Any advice for that??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exercise help
    By ali.franco95 in forum C Programming
    Replies: 3
    Last Post: 09-09-2011, 05:59 AM
  2. exercise in c
    By hugoguan in forum C Programming
    Replies: 1
    Last Post: 11-30-2010, 04:32 AM
  3. Exercise C, or for, or while or do while
    By vanina_18 in forum C Programming
    Replies: 1
    Last Post: 06-15-2010, 03:20 PM
  4. Exercise
    By bumfluff in forum C++ Programming
    Replies: 15
    Last Post: 04-21-2006, 12:18 PM
  5. K&R Exercise 1-14
    By Lee134 in forum C Programming
    Replies: 3
    Last Post: 02-16-2006, 11:20 AM