Quick Question

This is a discussion on Quick Question within the C++ Programming forums, part of the General Programming Boards category; Regarding Arrays and retrieval of Info If you have say Char Array[100] and fill this with a simple binary alphabet ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    82

    Quick Question

    Regarding Arrays and retrieval of Info

    If you have say Char Array[100] and fill this with a simple binary alphabet ie (H,P)

    Whats the best way to create another function which can call whats inside the array and then change each element so that H=1 and P=0, so i can then print this out?

    Any help is appreciated

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,439
    Can you please explain better what you want to do?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I was thinking that and wondering if he was meaning an enumerated list type thing.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    You need to use a pointer. Since a function can only return one value, you need a pointer in order to "get to" all of the values in an array.

    The same issue arises with C-style strings. If you read the Strings tutorial, you will notice that all of the functions in <cstring> use pointers.

    in fact, it would be a good idea to work through the all of the cprogramming.com tutorials and a good beginning C++ book before you start "playing around" too much.

    ... so i can then print this out?
    What operating system? Actual printing (to a printer) can be tricky on some operating systems. With Windows, you may have to use Win32 API functions & print via the Windows driver.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Basically (hopefully)

    Have user input a string of characters into an array, such as "HHPHHPHPHP".

    Then have another function take this Array, read it and change it so that H=1 and P=0, ie "1101101010"

    edit, sorry by print i was just meaning a simple "cout"

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    I'll have a read through the pointers section again, i wa looking over the arrays section and thought it would only return 1 element in the array and i couldn't find a way to take out everything in the array for example.

    Thanks for the help

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,439
    Well, I don't think you need explicit pointers. All you have to do is to traverse the array, read each element at a time and change it accordingly.

    Code:
    for(size_t i = 0; i != arr_size; ++i) {
        switch(array[i]) {
            case 'H':
                array[i] = 1;
                break;
            case 'P':
                array[i] = 0;
                break;
            default:
                break;
        }
    }
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Here's another pointer tutorial. There's a little section called "Pointers and Arrays". The example doesn't use a function, but maybe it will get you on the right track.

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Well, I don't think you need explicit pointers. All you have to do is to traverse the array, read each element at a time and change it accordingly.
    It depends on "where you are" and where the array is coming from (variable scope). Since Cdrwolfe wants to use a "another function", he needs a pointer (or a reference).

    In most programs, this is the case. A function is used to manipulate the array. The array is usually external to the function (not in-scope), so you need a pointer.

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Thanks for all the help guys, got some reading to do

    Just wish there was a chat function so i wouldn't have to make a new post everytime i wanted to ask a simple question

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

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