Some basic things

This is a discussion on Some basic things within the C Programming forums, part of the General Programming Boards category; I want to help a friend of mine who just started learning programming in C, but has problems catching up ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    24

    Some basic things

    I want to help a friend of mine who just started learning programming in C, but has problems catching up because he doesn't have any previous knowledge of it. I myself know vb.NET but never programmed in C, so I can't help.

    Some things that are asked:

    - A program which counts vowels in a given text using switch.case.
    (Similar to VB's "select case"? How to write this?)

    - A program that calculates powers (ex. 3^4 when 3 and 4 are given by the user) without using pow(a,b).
    I was thinking of writing something like A*((B-1)* "*A"), which would probably work in VB but how do you write this correctly in C?

    - A program in which the user enters an amount of numbers, then it returns the minimum, maximum and average value when -1 is entered.
    I asked another friend of me about this and he wrote down the following code, but we were unable to test it. Would it work?

    Code:
    #include <stdio.h>
    int main(void)
    {
    int min = 0;
    int max = 0;
    int sum = 0;
    int count_numbers = 0;
    
    int read_number = -1;
    scanf(“%d”, &read_number);
    
    int min = read_number;
    int max = read_number;
    int sum = read_number
    
    while (read_number != -1){
    if(read_number < min)
    min = read_number;
    else if(read_number> max)
    max = read_number;
    sum+= read_number;
    count_numbers++;
    scan
    f(“%d”, &read_number);
    }
    printf(“min %d”, min);
    printf(“max %d”, max);
    printf(“average %d”, sum/count_numbers);
    }
    scanf
    If there is anyone who would be willing to help in any way, by writing down the code (and if possible a little bit of explanation) I would be really grateful. Thanks a lot in advance!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,674
    It would be better if your friend joined the forum directly, so any key information doesn't get lost in translation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    24
    Quote Originally Posted by Salem View Post
    It would be better if your friend joined the forum directly, so any key information doesn't get lost in translation.
    I am sure that what I said is what is needed, and I will give him the link to this topic if there is anything that could be useful. I understand your concern, but don't worry!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    format of switch:
    Code:
    switch (EXPRESSION)
    {
         case: expression
              (STATEMENT)
         ...
    
         default:
              (STATEMENT)
    }
    Please note that a break statement needs to be placed after the statements if you don't want it to proceed to the ones after it. The switch statement basically jumps to a specific point in the code, so a break is needed most of the time. The default statement is when none of the cases are fulfilled.

    Good luck

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    For the powers, just make a loop that multiplies A by itself B times.
    Code:
    for (i=0; i < B; i++)
        A = A * A;
    NOTE: I wrote that really fast. There might be an error somewhere, but it should give you the basic idea.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    24
    Thank you for explaining that, Abda92. Unfortunately our knowledge is very limited. For example the switch case, you can enter "Case: a" for the first vowel, right? But what kind of statement could be used to have the program remember the amount of vowels, then add them together? I assume you can declare some kind of variable, but I don't know the code to do this. If you could explain that quickly, it'd be really helpful.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    Perhaps something like a loop and a switch:
    Code:
    std::string my_str; // We're assuming the text is stored in my_str
    int vowels = 0;
    for (int i = 0; i < strlen(my_str); i++) // Loop until we've reached the end of the string
    {
    	switch (my_str[i])
    	{
    		case 'A':
    		case 'E':
    		//...
    		case 'O':
    			vowels++;
    			break;
    		//default:
    	}
    }
    I'm not an expert on std::string, though.
    Last edited by Elysia; 11-10-2007 at 12:48 PM.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    24
    Yes, a loop may be a better idea, although using switch.case was asked for in the assignment. Thank you for writing it down! I hope we can get it to work with this, thank you for your time.
    Last edited by Nathalie; 11-10-2007 at 12:47 PM.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    This doesn't take into account lower case chars either. You could turn them into upper- or lowercase with functions, though, or check them separately.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    24
    Oh, okay. Better to check them separately I think because I wouldn't know how to write that function. As I said I have no knowledge of C.

    But if you write:

    Code:
    case 'A':
    case 'E':
    case 'O':
    ...
    then add the statement after that, it works for all of them, correct?

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    There are functions for that. Can't remember their names, I'm used to MFC classes.
    Anyway, to your question: yes, as long as you don't put break, it will continue down until it hits break and then abort the switch.
    VB automatically does break after each case I believe, but C/C++ doesn't.

    Code:
    std::string my_str; // We're assuming the text is stored in my_str
    int vowels = 0;
    for (int i = 0; i < strlen(my_str); i++) // Loop until we've reached the end of the string
    {
    	// If we assume my_str[i] == 'A'.
    	switch (my_str[i])
    	{
    		case 'A': cout << "A"; break;
    		case 'E': cout << "B"; break;
    	}
    	// Prints "A".
    	switch (my_str[i])
    	{
    		case 'A': cout << "A";
    		case 'E': cout << "B"; break;
    	}
    	// Prints "AB"
    }

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    24
    Yes, it does break after each case, that's why I wasn't sure. I think I get the code. vowels ++ means it will add +1 to the vowels integer, it seems? And can I use something similar to your code for the a^b exercise? (For example for printing the result, or any variables that need to be declared?)

    I hope I am not bothering you by all these questions, but everything you said so far has been really helpful already.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    Quote Originally Posted by Nathalie View Post
    vowels ++ means it will add +1 to the vowels integer, it seems?
    Yes, that is correct.

    And can I use something similar to your code for the a^b exercise? (For example for printing the result, or any variables that need to be declared?)
    Abda92 posted an excellent code. Basically just multiply againt the same variable X number of times:
    Code:
    int num = 10;
    // Let's do 10 ^ 5
    for (int i = 0; i < 5; i++) num *= num;
    Reads: While i < 5 (starting from 0), multiply num against num and store the result in num.

    I hope I am not bothering you by all these questions, but everything you said so far has been really helpful already.
    Not at all. That's why we're here - to help!

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    for (int i = 0; i < strlen(my_str); i++)
    Consider calculating strlen() outside of that loop -- it's rather expensive in time to compute for every iteration of the loop.

    Code:
    std::string my_str; // We're assuming the text is stored in my_str
    We're assuming we're in the C++ forum unfoundedly, eh?

    Code:
    scanf(“&#37;d”, &read_number);
    In C (and most other programming languages), those kind of quotes don't work. Your word processor might insert them for you, but you have to use the straight kind: "text".

    If your word processor does this, then don't write programs in it. Use an IDE like Dev-C++ instead. You'll get lots of other nice features like syntax highlighting (colours everywhere!), integrated compilation (e.g., press F9) and debugging (step through the source code inside the editor) and error messages.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Nov 2007
    Posts
    24
    Okay, thanks for explaining once again! One tiny question more: how do you print the result of the a^b thing?

    ETA: dwks, that means I have to replace every “&#37;d” by some other text? What kind of? I didn't write that code, I asked somebody else for help but he was unable to explain or go on any further.
    Last edited by Nathalie; 11-10-2007 at 01:20 PM.

Page 1 of 5 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. noob with basic q's
    By SimplyComplex in forum C++ Programming
    Replies: 8
    Last Post: 11-19-2006, 12:17 PM
  2. Basic things I need to know
    By maxorator in forum C++ Programming
    Replies: 53
    Last Post: 10-15-2006, 04:39 PM
  3. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  4. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 07:41 PM
  5. Basic Window Creation, Dev C++ 4.9.9.0 Linking Error
    By Tronic in forum Windows Programming
    Replies: 2
    Last Post: 11-27-2004, 05:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21