Please help me with arrays C++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Please help me with arrays C++

  1. #1
    Join Date
    Nov 2009
    Posts
    14

    Please help me with arrays C++

    OK guys i am writing a C++ program that involves a set of data such as:
    rIcK AbdDfabcBADabf
    jAMEs fdaBCADfBfacC

    now the first part of the line is a name and the second part are grades that i need to calculate at the end. the name i wil have to convert to regular such a rIcK to Rick. but m biggest issues is reading the data. becaue i know the name will have to be a string and the AbC.... will be an int array. so how do make a program that will recogninzed the input as the first part to be a string and the second to be an int. Please hep me

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Please help me with arrays C++

    No, "AbdDfabcBADabf" is not an int array but also a string. Read it in as a string, then convert it into an int array.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  3. #3
    Join Date
    Nov 2009
    Posts
    14

    Re: Please help me with arrays C++

    could you some me a example of the algorithm required because i am a beginner still.

  4. #4
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Please help me with arrays C++

    Quote Originally Posted by theeurostick View Post
    could you some me a example of the algorithm required because i am a beginner still.
    • Create an array the size of the string
    • for each character in the string
      • Convert the character to lowercase
      • Subtract 'a' from the character and convert to an int
      • Store the int at the place in the array corresponding to the location of the character in the string
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  5. #5
    Join Date
    Nov 2009
    Posts
    14

    Re: Please help me with arrays C++

    i don't see have making them all lower case and then subtracting it will help. this is what the assignment asks for :

    A 20 question multiple choice test was given to a group of students. The possible responses are a, b, c, or d.

    A file has been created that contains the following:

    * 1st line: The answer key consists of 20 lower case letters that represent the correct answers to the exam. There will be no blank spaces between the letters. Example: abcdabcdabcdabcdabcd
    * 2nd-last line: Each line will start with the name of a student. The name may be in a mixture of upper and lower case letters. The name will be followed by 1 blank space and then that student's responses to the test questions will be provided. There will not be any blank spaces between the responses, but they may be in a mixture of upper and lower case letters. An upper case letter should be considered correct if it matches the lower case equivalent in the key. Example: biLL aBcdAbCDabDcabDDABCD

    Design a C++ program that will

    * interactively prompt for and read the name of the input file
    * interactively prompt for and read the name of a file to write the output of the program to
    * write the following to the specified output file
    o your name, section #, assignment #
    o a report that displays the names of the students along with their test scores (see formatting specifications below)
    o the number of students who took the test with a label (maximum class size is 50)
    o the class average displayed with 3 digits to the right of the decimal and a label
    o the standard deviation of the test scores with 3 digits to the right of the decimal and a label
    o display the highest score and list the names of the students who received it

    Grading a Test
    Each of the questions on the test is worth 5 points. The test score is the # of correct responses multiplied by the point value of a question.

    Computation of Average and Standard Deviation
    If n = # of values, and datai = the ith value,

    Formatting Requirements for Output

    * Arrange information in the grade report into 2 columns with headings. The first columns should be labeled "NAME" and the second column "SCORE".
    * Reformat names so that the first letter is capitalized and the remaining letters are lower case.
    * Left justify the names (maximum length is 15 characters). Right justify the scores and display with no decimals (maximum score is 100)
    * Leave a blank line before and after the statistics (student count, average and standard deviation).
    * When displaying the list of students with the high score, each name should be on a separate line.

    Input File Assumptions/Definitions

    * maximum number of students will be 50
    * the name and responses for each student will be on a separate line of the file
    * each line of the file will be terminated by a linefeed
    * the input file will exist and not be empty.

    Sample terminal session and output
    [lee@bobby]$ more students
    abcdabcdabcdabcdabcd
    jOnEs ABCDabcdABCDabcdABCD
    sMITH dcbaABCDabcdABCdabCa
    ADams AAAAAAAAAAAAbBBBBBBB
    mARTINSon ABCDABCDABCDABCDABdb
    schWarTz aabbcccdabcdabcdabcd
    blaCk ABCDABCDABCDABCDAbcd
    [lee@bobby]$ g++ eleven.cpp
    [lee@bobby]$ ./a.out
    Enter name of input file
    students
    Enter name of output file
    gradesummary
    [lee@bobby]$ more gradesummary
    Lee Misch Assign #11 Sec #_

    NAME SCORE
    Jones 100
    Smith 75
    Adams 25
    Martinson 90
    Schwartz 75
    Black 100

    # students: 6
    Average: 77.500
    Standard deviation: 25.617

    High score of 100 earned by
    Jones
    Black

    Other Requirements - Failure to adhere to the following requirements will result in major point deductions.

    * The design of the program must use functions to modularize the code. Try to develop a function for each task to be performed.
    * No global variables may be used in the program.
    * Use parameters to pass values between functions.
    * The program may only read the content of the file once.

    Required documentation (30 pts)

    * name, section #, assignment # in subject line of email (2 pts)
    * state your name, section #, and assignment # in a comment (1 pt)
    * provide a description of what the program will do (clearly state that input will be read from a file, what the content of the file is expected to be, and what output will be generated) (12 pts)
    * provide brief descriptions of the major variables and constants used in the program (5 pts)
    * for each function, provide a brief description that includes what it does and what is passed in/out and document major local variables (10 pts)

    After modifying your program file in any way, always recompile and run your program.

    ______My biggest problem is splitting the array everything else i could possibly easily do

  6. #6
    Join Date
    Nov 2009
    Posts
    14

    Re: Please help me with arrays C++

    #include <iostream>

    #include <iomanip>

    #include <cctype>

    #include <fstream>

    #include <string>

    #include <cmath>

    using namspace std;

    int main ()
    {
    if stream infile;

    string file;

    cout << "Please enter name of input file" << endl;

    cin >> file ;

    infile.open(file.c_str());




    return 0;
    }


    this is what i have so far for this can anyone tell me if this is a wrong way or /and possible tell the what i could do next

  7. #7
    Join Date
    Apr 1999
    Posts
    27,424

    Re: Please help me with arrays C++

    Quote Originally Posted by theeurostick View Post
    #include <iostream>

    #include <iomanip>

    #include <cctype>

    #include <fstream>

    #include <string>

    #include <cmath>

    using namspace std;

    int main ()
    {
    if stream infile;

    string file;

    cout << "Please enter name of input file" << endl;

    cin >> file ;

    infile.open(file.c_str());




    return 0;
    }


    this is what i have so far for this can anyone tell me if this is a wrong way or /and possible tell the what i could do next
    Please use code tags when posting code. Once you use code tags, the code will look like this:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cctype>
    #include <fstream>
    #include <string>
    #include <cmath>
    
    using namspace std;
    
    int main ()
    {
        ifstream infile;
        string file;
        cout << "Please enter name of input file" << endl;
        cin >> file ;
        infile.open(file.c_str());
        return 0;
    }
    Second, please don't post your entire assignment. No one is going to read through that whole thing and post a step-by-step solution.
    * interactively prompt for and read the name of the input file
    * interactively prompt for and read the name of a file to write the output of the program to
    * write the following to the specified output file
    So why not finish up these steps? You have a compiler, so why not do these steps and test your program?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 28th, 2009 at 11:21 PM.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,424

    Re: Please help me with arrays C++

    Quote Originally Posted by theeurostick View Post
    i don't see have making them all lower case and then subtracting it will help.
    As treuss mentioned, "AbdDfabcBADabf" is not an array of int. So given this string, what is this supposed to be translated to? Don't describe what it should be, actually tell us what that string should be translated to.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Nov 2009
    Posts
    14

    Re: Please help me with arrays C++

    the AbdDfabcBADabf i supposed to be used as a set of possible choices of a multiple choice test. the first thing that the file has is the key to the answers then the file contains a persons name and his answer to the test and so on tot he next person. each correct answer is worth 5 pionts wrong anwser no pionts

  10. #10
    Join Date
    Apr 1999
    Posts
    27,424

    Re: Please help me with arrays C++

    Quote Originally Posted by theeurostick View Post
    the AbdDfabcBADabf i supposed to be used as a set of possible choices of a multiple choice test. the first thing that the file has is the key to the answers then the file contains a persons name and his answer to the test and so on tot he next person. each correct answer is worth 5 pionts wrong anwser no pionts
    You don't need any input to finish most of this assignment. All you need to do is write functions that test characters and return an integer score.

    Start here:
    Code:
    #include <string>
    #include <iostream>
    
    int GetStudentScore(const std::string& student_answers, // student's answers
                                     const std::string& answer_key // answer_key
    )
    {
        int student_score = 0;
        // write code that goes through the strings and return the student's score
    
       //...
       return student_score;
    }
    
    using namespace std;
    
    int main()
    {
       std::string answers;
       std::string student_test;
       cout << "Enter answer key: ";
       cin >> answers;
       cout << "Enter student answers: ";
       cin >> student_test;
    
       cout << "The student's score is " << GetStudentScore(student_test, answers);
    }
    Then you test this program. Once you have written the code to give a numeric score given two strings, then and only then should you move on.

    And as stated, to get the score, write a loop that goes through each character of the student's answer and the answer key.

    In the loop:

    Compare the lower case version of the answer key character and the student's answer character. If they match, then add 5 points to the score.

    I don't see anywhere an "int array" is needed.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Nov 2009
    Posts
    3

    Re: Please help me with arrays C++

    Just read the answer and the key as two strings then use a loop to compare the lowercase versions of their characters (you can just use tolower() http://www.cplusplus.com/reference/c...ctype/tolower/ ). If the two letters match then simply add 5 as Paul said to the score of the student

  12. #12
    Join Date
    Nov 2009
    Posts
    14

    Re: Please help me with arrays C++

    thank you for your guyes help i found out what i needed to do all it was is somwthing like this
    string name;
    string key;
    string answers;

    infile >> key>> name >> answers;

    but its ok i got it

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center