Thread: Not enough variables.

  1. #1
    UnknownAmateur Programmer
    Join Date
    Sep 2008
    Location
    Were definitly not in Kasas anymore...
    Posts
    25

    Not enough variables.

    I want to right a program that will take any set of numbers and average them. I need a lot of variables to do that and I want to know if I can have the program declare variables as the user enters in numbers all having different names. Can you show me how?

    C-Compiler

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You want a "map".
    Google up some source code, because there isn't any standard implementation in C (as opposed to C++).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    India
    Posts
    2
    You can ask the user to enter the total number of integers he wants to take the average of..store that in a variable....then you can dynamically allocate an integer array of that size using the "new()" function.....
    then you will have an array of integers to work with...take sum of all elements and divide.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    "new" isn't available in C. Only malloc is.
    The problem with that is that it won't allow you to "name" your variables.
    But if you just want to average the sum, sure it works.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    What is the point for that? When you compile the program and a user executes it the variables don't have any purpose. They don't have "names", they are just memory locations. In other words, what purpose could it serve for the user to name variables of the program?

    The best way to do this is use a pointer which will start with dynamically allocating one int. Then it will realloc() each time the user enters a new variable one more memory space. If you want it to have a name, more likely a "virtual name" then you can create a struct like:
    Code:
    struct virtualVariable {
       int value;
       char* name; //or char[MAX_CHARACTERS];
    }
    and again have a pointer to a *virtualVariable pointer that will realloc() structs. Each time you can also save the name for your virtual variable.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by C_ntua View Post
    The best way to do this is use a pointer which will start with dynamically allocating one int. Then it will realloc() each time the user enters a new variable one more memory space. If you want it to have a name, more likely a "virtual name" then you can create a struct like:
    Code:
    struct virtualVariable {
       int value;
       char* name; //or char[MAX_CHARACTERS];
    }
    and again have a pointer to a *virtualVariable pointer that will realloc() structs. Each time you can also save the name for your virtual variable.
    This is the map's purpose.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    But why use a map?
    You only need the values of the variables, not the names of the variables.
    A dynamic array of int's, double's or whatever is all you need.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    Quote Originally Posted by cpjust View Post
    But why use a map?
    You only need the values of the variables, not the names of the variables.
    A dynamic array of int's, double's or whatever is all you need.
    Probably because his question seemed to lean towards that route. I'm questioning whether it's what he really wants though.

    From some other posts he's made, it seems like he's fairly new to programming; I think what he really wants is just a dynamic array. If this is the case, do some research on dynamic arrays, and realloc

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    One of the things I miss most about Perl is hashes with named elements. A Perl hash beats setting up a map type function with arrays and structs any day.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Unless you have a working implementation. C can be just as effective as Pearl or better.
    Or better yet, if you use C++, then you have your quick map ready to use, since it's in the standard library.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You need two variables. One to keep the number of values entered, and the other to keep the accumulated sum of all every value entered, such as
    Code:
    int count = 0 , accum = 0 ;
    
    do { 
    	count++ ; 
    	accum += users_latest_value ; 
    
    } while (input is a number ) ; 
    
    printf("The average is %d\n", accum / count) ;
    Mainframe assembler programmer by trade. C coder when I can.

  12. #12
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Yeah I can't see the need for anything else than constant memory either. This seems like a classical XY problem where a person has a problem X and though of a solution Y and asks about Y instead of X. Just sum them up as you read them, then divide by the amount read.

    As for mappings, unless you have some specific naming scheme the indexes of an array can serve as the mapping to values.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, as OnionKnight says, the problem can be solved with 3 variables: The number entered, a count of number of values entered, and a sum of the values so far. (plus possibly one more for loop counting if we use a "how many numbers" question at the beginning, rather than a "enter <X> to end" [where X typically is -1, 0 or some such] "sentry ending loop").

    Unless you also need to do other things with the values entered, there is no need to save all the values entered.

    As to the discussion about maps and Perl hashing. As Perl is written in C, I doubt that C would be slower than Perl, given an equal implementation. I have seen mentions of a hashmap that is supposed to be better than std::map - but it takes a pretty huge table before the difference makes any noticable difference (like hundreds of thousands of entries or more - as map is O(logn), so the number of compares to search a 1000 entries is 10 - not exactly an ardeous task in a modern computer).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by matsp
    As to the discussion about maps and Perl hashing. As Perl is written in C, I doubt that C would be slower than Perl, given an equal implementation. I have seen mentions of a hashmap that is supposed to be better than std::map - but it takes a pretty huge table before the difference makes any noticable difference (like hundreds of thousands of entries or more - as map is O(logn), so the number of compares to search a 1000 entries is 10 - not exactly an ardeous task in a modern computer).
    My impression is that Viper187's point is simply that C lacks a map construct that is provided by either the core language or the standard library, and that even if one was provided, Perl hashes provide some syntactic sugar that will still allow it to trump a C version in terms of "writability".
    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

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    ...I have seen mentions of a hashmap that is supposed to be better than std::map - but it takes a pretty huge table before the difference makes any noticable difference (like hundreds of thousands of entries or more - as map is O(logn), so the number of compares to search a 1000 entries is 10 - not exactly an ardeous task in a modern computer).
    Now that's a pretty big assumption, seeing as all compiler vendors are allowed to implement their std::map differently - so there is no "one" implementation of std::map, so I cannot think that is a valid statement or fact.
    Is it faster than ALL std::map implementations or just one? If it's just one, then it's would probably just be bragging.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. Craps Program with Local Variables
    By tigrfire in forum C Programming
    Replies: 12
    Last Post: 11-09-2005, 09:01 AM
  5. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM