CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: arrays

  1. #1
    Join Date
    Mar 2009
    Posts
    78

    arrays

    Hey, I am kinda having problems with the following...

    The first array will have between 4 to 8 members of integer
    data type and will be the IQs (Intelligent Quotient - usually
    values between 80 and 150) for those people.

    I can't find a shorter way to do this. I mean usually you do it like int ages[5] = {49,48,26,19,16}; etc. but there has to be something for a wide range of numbers like here. And I have no idea how to include the 4 to 8 members in this

    Maybe this format?

    Code:
    ages[0] = 49;
          
    
    
            ages[1] = 48;
          
    
    
            ages[2] = 26;
          
    
    
            ages[3] = 19;
          
    
    
            ages[4] = 16;

    But then I don't quite know how to assign the IQ value to them.

  2. #2
    Join Date
    Apr 2008
    Posts
    725

    Re: arrays

    what? I think you need to include more of the homework question. oh wait...

  3. #3
    Join Date
    Mar 2009
    Posts
    78

    Re: arrays

    That is it. Needs to be single dimension array.

  4. #4
    Join Date
    Aug 2007
    Posts
    858

    Re: arrays

    The first array will have between 4 to 8 members of integer
    An array can't have "between 4 and 8 members". You can have an array with 8 members and only use 4 of them, if you so choose.

    Beyond that it isn't really clear what you need to do. Are you storing age or IQ, or both? Are they supposed to be stored in one array or two?

  5. #5
    Join Date
    Mar 2009
    Posts
    78

    Re: arrays

    Well, there has to be a second array that has 6 members of floating point data
    type and will be the amount of cash money that each of 6 people have in their pockets . Example: I have $5.32 in my pocket.

    Then I gotta allow the person running the program to change the IQ of the 2nd member of the array.
    At the end just display the values for each array with a for loop using the sizeof operator to determine the number of members in the array for the test expression.
    That's basically it. So I guess it needs to store them.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: arrays

    Quote Originally Posted by XodoX View Post
    At the end just display the values for each array with a for loop using the sizeof operator to determine the number of members in the array for the test expression.
    Oh God, is that a requirement? It's bad enough when newbies abuse the sizeof() operator trying to get array lengths that they could just as easily store in a variable somewhere; now teachers are promoting that practice?

  7. #7
    Join Date
    Mar 2009
    Posts
    78

    Re: arrays

    Quote Originally Posted by Lindley View Post
    Oh God, is that a requirement? It's bad enough when newbies abuse the sizeof() operator trying to get array lengths that they could just as easily store in a variable somewhere; now teachers are promoting that practice?
    Yes, it is.

  8. #8
    Join Date
    Mar 2009
    Posts
    11

    Re: arrays

    Forgive me if I'm being redundent, but you are suposed to have an array cabable of carring up to eight integers for IQ's and an array of six floats for the spare change. The program allows you to make changes to the IQ array's second element and display a sizeof() in a for loop to display both arrays size.

    Therefore your question is how to asign your elements an IQ? (got this from the first post)

    If so, you are correct, this will create and assign a number to the IQ array.

    int arrayIQ [8]={0,0,0,0,0,0,0,0};
    arrayIQ[2]=100;

    of corse, you have to let the human enter the actuall nuber that it becomes correct? So
    insead of 100 you would use a variable that user input would set.

    int arrayIQ[8]={0,0,0,0,0,0,0,0};
    int variableForIQ_subTwo=0;


    cin>>variableForIQ_subTwo;

    arrayIQ[2]=variableForIQ_subTwo;



    If not this, I don't understand your question.

  9. #9
    Join Date
    Aug 2007
    Posts
    858

    Re: arrays

    Quote Originally Posted by XodoX
    Well, there has to be a second array that has 6 members of floating point data
    type and will be the amount of cash money that each of 6 people have in their pockets . Example: I have $5.32 in my pocket.

    Then I gotta allow the person running the program to change the IQ of the 2nd member of the array.
    At the end just display the values for each array with a for loop using the sizeof operator to determine the number of members in the array for the test expression.
    That's basically it. So I guess it needs to store them.
    Ok, then which part is it you're having trouble with?

    Quote Originally Posted by ehoi
    If so, you are correct, this will create and assign a number to the IQ array.

    int arrayIQ [8]={0,0,0,0,0,0,0,0};
    You can actually just do

    Code:
    int arrayIQ[8] = { 0 };
    Which will initialize all of the values in the array to zero.

  10. #10
    Join Date
    Mar 2009
    Posts
    78

    Re: arrays

    So the one for the six members works the same way?

    float arrayIQ[6] = { 0 };
    Last edited by XodoX; April 18th, 2009 at 06:10 PM.

  11. #11
    Join Date
    Mar 2009
    Posts
    78

    Re: arrays

    Ok, now I got it.

    It's supposed to show about 8 IQ's. Then it displays a message saying that the IQ of the 2nd person has changed, and you need to enter the new IQ. Then it shows the 8 IQ's again, but now of course it gets replaced by the newly entered IQ.

    Then it needs to show 5 amounts of cash that these people have ( I know, it's random)

    I tried it, but it dosen't compile yet


    Code:
    // Headers and Other Technical Items
    
    #include <iostream>
    using namespace std;
    
    // Function Prototypes
    
    void pause(void);
    
    // Variables
    
    int iq[8]={80,90,100,110,120,120,140,150};
    float  cash[] = {'3.45','1.84','1.0','3.10','.2.71','0.89'};
    
    int   x;          // Used as a flag to control for loops
    
    //******************************************************
    // main
    //******************************************************
    
    int main(void)
      {
      cout << "\nThe oiginal IQ's:" << endl;
      for(x = 0; x < 5; x++)
        {
        cout << iq[x] << endl;
        }
    
      cout << "\n\nThe second person became smarter, what is his  new IQ ->: ";
      cin >> iq[2];
    
      cout << "\n\nThe revised IQ:" << endl;
      for(x = 0; x < 5; x++)
        {
        cout << iq[x] << endl;
        }
    
      pause();
    
      cout << "\n\nThe cash these people have :" << endl;
      for(x = 0; x < sizeof cash / sizeof cash[0]; x++)
        {
        cout << cash[x] << endl;
        }
    
      pause();
      return 0;
      }
    
    //******************************************************
    // pause
    //******************************************************
    
    void pause(void)
      {
      cout << "\n\n";
      system("PAUSE");
      cout << "\n\n";
      return;
      }
    It changes the 3rd IQ, and something with the cash amouts dosen't work either If I wanted to say "I have xx.x cash in my pocket" , would I have to put it down in the array each time?
    Last edited by XodoX; April 19th, 2009 at 09:51 PM.

  12. #12
    Join Date
    Mar 2009
    Posts
    51

    Re: arrays

    Quote Originally Posted by Lindley View Post
    Oh God, is that a requirement? It's bad enough when newbies abuse the sizeof() operator trying to get array lengths that they could just as easily store in a variable somewhere; now teachers are promoting that practice?
    Not to mention holding financial information in floats!

  13. #13
    Join Date
    Apr 2008
    Posts
    725

    Re: arrays

    Quote Originally Posted by XodoX View Post
    ...
    It changes the 3rd IQ, ...
    you really can't figure out, even by trial and error, how to change that so it changes the third IQ?

    also, sigle quotes are for const chars, e.g.
    Code:
    char letterR = 'R';
    So
    Code:
    char foo = 'bar';
    will probably not give what you expect. Therefore I suggest you revisit
    Code:
    float  cash[] = {'3.45','1.84','1.0','3.10','.2.71','0.89'};
    just like your compiler tells you too
    Last edited by Amleto; April 20th, 2009 at 12:25 PM.

  14. #14
    Join Date
    Mar 2009
    Posts
    11

    Re: arrays

    If you remember, arrays always start counting at zero, therefore iq[2] is the THIRD element. It goes: iq[0] iq[1] iq[2] etc. Also, make sure you don't place a [number] higher than the array. (like iq[10]) It WILL use it. This usually destroys something else you stored to in doing it.
    Last edited by ehoi; April 26th, 2009 at 07:43 PM.

  15. #15
    Join Date
    Mar 2009
    Posts
    78

    Re: arrays

    I got another questiom, I'll just post it here:

    Code:
    //******************************************************
    // display_array
    //******************************************************
    
    void display_array(int things[], int array_size)
      {
    
      cout << "\n************************************";
      for (int i = 0; i < array_size; i++)
        {
        cout << "\nOffset: " << setw(2) << i;
        cout << " Memeber: " << setw(2) << i+1;
        cout << " value is: " << setw(3) << things[i];
        }
      cout << "\n************************************";
    
      return;
      }
    That part is displaying an array from another code.

    I need to change the display function to list the items horizontally with two spaces between them. What's the best way to do this?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured