Thread: C++ Programming Question Arrays

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    1

    C++ Programming Question Arrays

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    
      int scores[48] =
          { 70, 66, 87, 90, 80, 95, 76, 66, 80, 61, 67, 73, 89, 89, 88, 40, 91,
    91, 77, 88, 88, 67, 76, 89, 73, 81, 82, 90, 73, 66, 90, 93, 88, 80, 89, 87 };
    
      string student[16] =
          { "Abbey", "Cecilla", "James", "Todd", "Hellen", "Pat", "Richard",
    "Alex", "Nicky", "Nora", "Paul", "Arthur", "Emillia", "Liv", "Luca", "Teddy" };
    
      cout << scores[0] << student[3];
    
      return 0;
    }
    How do I link the strings to the integers?
    Tx.
    I am totally stumped.
    Last edited by Salem; 11-08-2014 at 11:49 PM. Reason: removed font abuse

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You mean the fact that there are three scores per student?

    Level 1
    Code:
    int scores[16][3] = {
      { 1, 2, 3 },
      { 4, 5, 6 },
    };
    Level 2
    Code:
    struct student {
      string name;
      int scores[3];
    };
    Level 3
    Code:
    class student {
      private:
        string name;
        int scores[3];
      public:
        // constructor, setters, getters etc.
    };
    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.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    This would be a prefect job for a hashtable.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No need for a hashtable/tree for a such a simple job of mapping integers from 0...N to an array. In the general case, though, where the range is N...M where N and M are different for every lookup or more types other than integral types, I am willing to agree, though.
    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
    Join Date
    Jul 2014
    Location
    Central Arizona
    Posts
    61
    Kevin Konopka, This is not a big deal, but are you aware of the fact that your array only shows 36 element, rather then 48 you called out? Therefore your going to have 12 elements with a value of 0 (zero)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C programming arrays
    By sgk1498 in forum C Programming
    Replies: 6
    Last Post: 03-10-2014, 04:40 PM
  2. 2D arrays programming, please help
    By mfskratch in forum C Programming
    Replies: 4
    Last Post: 11-01-2007, 12:41 PM
  3. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM
  4. Question sorting character arrays: C programming
    By NeoSigma in forum C Programming
    Replies: 3
    Last Post: 05-23-2003, 09:28 PM