newb needs help again.

This is a discussion on newb needs help again. within the C++ Programming forums, part of the General Programming Boards category; I need to to the following and I'm still pretty new to coding. Our teacher has asked us to... Formulate ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    17

    newb needs help again.

    I need to to the following and I'm still pretty new to coding. Our teacher has asked us to... Formulate a C++ program solution that does the following. It holds in 2 parallel arrays, the values for the day# in the first array and in the second array the temperature for the corresponding day in the first array. These 2 arrays should only be declared after the size for each array has been specified by the user. Input for each array should come from the user. Lastly, the user should then be allowed to specify the day the user wants to find a temperature value for. In order to do this, the first array should be traversed looking for the matching day element, and from that matching index, find the corresponding temperature in the other array. That temperature value should be printed out. Usage of Loops is an absolute must in this Lab.

    I'm unsure how to get started on this seeing the array has to be user defined? Any push in the right direction would be appreciated.

    Thanks!

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    You can use dynamic memory allocation
    Code:
    int userInputValue = 0;
    
    //get user input
    
    int *userSpecifiedArray = new int[userInputValue];
    
    //do operations
    
    delete [] userSpecfiedArray;
    Or you could use std::vector, But since this is a school assignment, ussually teachers don't like those even though thats what we would use
    Woop?

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    17
    Tank you very much.

    Another thing I don't understand is the user will enter in the number of day's they want to view. that would populate the size of one array but what about the array for the temps? How will that array get populated with temperatures? should't the array for temps be the same as what the user specified for the number of days? Where will it get it's values?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    "Input for each array should come from the user."

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    And since they're parallel arrays - you should make them the same length...

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    17
    so I'm thinking program would ask user to enter in how many cities they want to view. User inputs a number. Then run a loop that has them enter in a temp the number of times they entered in for number of cites. Would that fill in the the 2nd array?

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    17
    OK here's what I have so far. I have it accepting input from the user. But i"m getting lost when it comes to accessing the arrays.

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int day = 0;
        int temp = 0;
        int *numDay = new int[day];
        int *temps = new int[temp];
    
       cout << "Enter the amount of cities you would like to view: ";
       cin >> day;
       
       for (int x=0; x<day; x++)   
       {
           cout << "Enter a temp: ";
           cin >> temp;
       }
       if (numDay[x] == day)
          {
           numDay = day[x];
           temps = temp[x];
           cout<<"day[x]: " <<"temp[x]: ";
           }
    
    }
    am I anywere in the ballpark?

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    You are close.

    Should be:
    Create holder city variable
    Get user input for city variable
    Allocate memory from input variable.

    Also. In your loop you want to actually modify the array value for the temp for the current index.
    Last edited by prog-bman; 02-11-2009 at 03:45 PM.
    Woop?

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    You MUST free everything you allocate.
    So for every
    int* numDay = new int[day];
    int* temps = new int[temp];
    You must also do
    delete [] numDay;
    delete [] temps;

    Also, you need to think over your design. Pseudo code and flowcharts help.
    Like:
    numDay = day[x];
    temps = temp[x];
    But temp is not an array!!
    And you haven't even filled anything in the array day yet!!
    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. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-22-2008, 11:37 PM
  2. Dogpile the newb!
    By lindy in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 05-23-2008, 08:17 AM
  3. Total newb directx invisable geometry question
    By -pete- in forum Game Programming
    Replies: 5
    Last Post: 08-13-2006, 01:45 PM
  4. Newb C++ Programmer
    By Philandrew in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2004, 08:44 PM
  5. if your not newb you can help me
    By Klinerr1 in forum C++ Programming
    Replies: 6
    Last Post: 05-05-2002, 12:09 AM

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