Thread: little help with a C exercise

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    6

    little help with a C exercise

    hey i am a beginner and really stuck in assignment:

    Display the alphabet on the screen, one letter on each line. Next to each letter, each line should also contain an average (obtained through the use of a running total) of all letters displayed in the left-hand column up to that point.

    We can only use the running total and a total of 3 Printf statement in the whole program.

    The output of program is suppose to look like this :

    a a
    b a
    c b
    d b
    e c
    f c
    g d
    h d
    i e
    j e
    ...until
    y m
    z m


    This is what i have coded so far but i am just confused

    Code:
    #include <stdio.h>
    int main()
    {
    char columm1 = 0;
    char total = 0;
    
    int i = 0
    int total = 0
    for (i = 0; i < 27; i++)
    {
    printf("%c, %c /n", columm1, total);
    columm1++;
    
    
    }
    return 0;
    }
    


    Please help me with what i have done wrong
    Thnx

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Next to each letter, each line should also contain an average (obtained through the use of a running total) of all letters displayed in the left-hand column up to that point.
    That's totally not what an average is.

    It seems like you may as well have two variables step through the alphabet, but one of them steps twice as slowly. To be completely honest, I have no idea how a running total is or an average is going to be that slow. I'm also not used to munging numerical output, like an average, into letter output.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    6
    Quote Originally Posted by whiteflags View Post
    That's totally not what an average is.

    It seems like you may as well have two variables step through the alphabet, but one of them steps twice as slowly. To be completely honest, I have no idea how a running total is or an average is going to be that slow. I'm also not used to munging numerical output, like an average, into letter output.
    if you notice the letter in this assignment for eg 'a' stands for 97 in ASCII table , similarly 'b' stands for 98 ....now look at it from that point of view and you will understand
    char Average
    97 97
    98 97 ( average is 97.5 but teacher told us not to worry about decimal)
    99 98
    99 98
    and so onn...

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

    However, I would insist that characters are not meant to be numbers in a mathematical sense.
    Last edited by whiteflags; 09-17-2013 at 10:22 PM.

  5. #5
    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.

  6. #6
    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??

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Variables you have:

    - columm1 - character to print for the first column
    - total - presumably holds a running total of every "columm1" value encountered.

    So what are you missing to calculate an average?

    A total is no good if you don't know how much to divide it by. And if you want to preserve both of these values for further calculations, then the resulting average should probably be stored in a separate variable (perhaps called "column2" - note the corrected spelling of column).

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    ROFL: "note the corrected spelling of column". ROFL

    Ding, Ding, Ding! Grammar Nazi detected!



    Welcome!

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Adak View Post
    ROFL: "note the corrected spelling of column". ROFL

    Ding, Ding, Ding! Grammar Nazi detected!
    Nein!

    Hey, just trying to reduce the chances of the OP coming back with an "undeclared identifier" problem. Besides, there are a lot more ways to spell a word wrong than there are to spell it right. It's generally easier to be consistent with a variable name spelled correctly than it is with one spelled wrong.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Adak View Post
    ROFL: "note the corrected spelling of column". ROFL
    Ding, Ding, Ding! Grammar Nazi detected!
    That's my job! And it's spelling, not grammar. Anyway, why not learn to spell and write properly? If you don't you sound like an idiot. Intelligence is a lot more than simply writing programs.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It is just a different encoding -> 97 == 0x61 == 01100001 == 0141 == ASCII 'a'. All are equally valid representations of the same numerical value.
    It's not something people are meant to care about though. I mean, if the output is what's important, there are other ways to do it that aren't dependent on encoding. Weird teaching lessons are afoot.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The issue I have is that the notion of an average of a set of letters makes sense to me if say, we consider median or mode, but not really if we consider mean, yet the teacher's instruction is to use the mean of their values in the character set. Like, the average of 10 As and 9 Cs might be A, but saying it is B sounds weird to me. That said, in this case all the letters of the alphabet are in use, one each, so the mean is (nearly) equivalent to the median.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    I dunno about the programming exercises teachers set. This one seems crazier than most :-o

    Since the problem appears to have been solved I'll paste some code below. Note that both methods in the code assume ASCII character encoding so they're really using implementation defined behaviour. Even if you replace 'a' with 97 and loop over say 0...26 both these methods using the implementation defined behaviour. To avoid that you'd have to have an array that describes the character encoding (ignoring for a moment that I don't entirely know what's being averaged and why).

    IMO, it's an insane exercise that doesn't make any sense, but perhaps I'm missing something.

    Code:
    #include <stdio.h>
    
    void averageMethod1(void)
    {
        int total = 0, count = 0;
        int i;
        
        /* Note that testing for i <= 'z' and also adding 1 to
         * 'a'..'z'-1 isn't portable since C makes no guarenteee
         * that characters are sequential or that 'a' < 'z'.
         * (The exception is the characters '0', '1'... '9')
         */
        for (i = 'a'; i <= 'z'; i++) {
            total += i;
            count++;
            printf("%c %c\n", i, total/count);
        }
    }
    
    void averageMethod2(void)
    {
        int total = 0, count = 0;
        int i;
        
        for (i = 'a'; i <= 'z'; i++) {
            total += i - 'a';
            count++;
            printf("%c %c\n", i, 'a'+total/count);
        }
    }
    
    int main(void)
    {
        puts("Method A");
        averageMethod1();
        
        puts("Method B");
        averageMethod2();
        
        return 0;
    }

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    IMO, it's an insane exercise that doesn't make any sense, but perhaps I'm missing something.
    The difficult part is that the teachers have to keep coming up with new exercises - something that hasn't been solved on CBoard, in the last year.

    Chars are just number values - you take a running average, and get the corresponding letter. Only need to put the teacher in therapy for a short while. <smile>

  15. #15
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Adak View Post

    Chars are just number values - you take a running average, and get the corresponding letter. Only need to put the teacher in therapy for a short while. <smile>
    Yep, they're just numbers but implementation defined numbers which makes me an angry bear lol


    Code:
    #include <stdio.h>
    
    static const unsigned char encoding[] = {
         97,  98,  99, 100, 101, 102, 103, 104, 
        105, 106, 107, 108, 109, 110, 111, 112, 
        113, 114, 115, 116, 117, 118, 119, 120, 
        121, 122
    };
    
    #define LETTER_COUNT (sizeof encoding / sizeof encoding[0])
    
    void averageChars(void)
    {
        int total = 0, count = 0;
        int i;
        
        for (i = 0; i < LETTER_COUNT; i++) {
            total += i;
            count++;
            printf("%c %c\n", encoding[i], encoding[total/count]);
        }
    }
    
    int main(void)
    {
        averageChars();
        
        return 0;
    }
    Edit: Hmm. That coding array won't work. I really need a struct that has the encoding you want to average and also the output encoding and have an array of those structs. Oh well.
    Last edited by SirPrattlepod; 09-18-2013 at 10:29 PM.

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