GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Apr-2009, 09:16
missy missy is offline
Awaiting Email Confirmation
 
Join Date: Mar 2009
Posts: 9
missy is on a distinguished road

Need help with dynamic arrays!


I have to rewrite this program using dynamic arrays. I am having a difficult time understanding dynamic arrays. How might I do this? Here is the code any help at all is appreciated! Thank you
CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

const int NO_OF_QUESTIONS = 20;
 
char examGrade(int score);

int main()
{
    char keyString[NO_OF_QUESTIONS + 1];
    char answerString[NO_OF_QUESTIONS + 1];
    char *ID.ptr;
	ID_ptr = new int[10];

    char ch;

    int score;
    int len;
    ifstream infile;

    int i;

	infile.open("file.txt");
    if (!infile)
    {
        cout << "Cannot open the input file. Program terminates!"
             << endl;
        return 1;
    }

    cout << "Processing Data" << endl;
    infile.get(keyString, 21);

    cout << "Key: " << keyString << endl;
    
    infile >> ID;
    infile.get(ch);
    infile.get(answerString, 21);

    while (infile)
    {
        len = strlen(answerString);
        for (i = len; i < 20; i++)
            answerString[i] = ' ';

        answerString[20] = '\0';
        
        score = 0;
        
        for (i = 0; i < 20; i++)
            if (answerString[i] != ' ')
                if (answerString[i] == keyString[i])
                    score = score + 2;
                else
                    score = score - 1;

        cout << ID << " " << answerString << "  " << score
             << "  " << examGrade(score) << endl;
        infile >> ID;
        infile.get(ch);
    infile.get(answerString, 21);
    }

    cout << endl;

    infile.close();

    return 0;
}

char examGrade(int score)
{
    switch (static_cast<int>(score / 40.0 * 10))
    {
    case 0: 
    case 1: 
    case 2: 
    case 3:
    case 4: 
    case 5: 
        return 'F';
    case 6: 
        return 'D';
    case 7: 
        return 'C';
    case 8: 
        return 'B';
    case 9: 
    case 10: 
        return 'A';
    default: 
        return 'F';
    }
} 
  #2  
Old 07-Apr-2009, 09:40
dlp dlp is offline
Member
 
Join Date: May 2006
Posts: 157
dlp has a spectacular aura about

Re: Need help with dynamic arrays!!


What is it you don't understand? Does your code work? If not, what's wrong with it?
  #3  
Old 07-Apr-2009, 09:52
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Need help with dynamic arrays!!


Since int aname[10] is really a pointer and you access the 4th element as aname[3] a dynamic array can be seen as the same thing.
aname = new int[10] loads a pointer into aname and the values can be accessed exactly the same way.

IOW, int aname[10] loads the address of an array into the variable aname before the program starts, but aname = new int[10] loads the same pointer while the program runs.

Just remember you need to free the dynamic array when finished with it.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #4  
Old 07-Apr-2009, 10:07
missy missy is offline
Awaiting Email Confirmation
 
Join Date: Mar 2009
Posts: 9
missy is on a distinguished road

Re: Need help with dynamic arrays!!


yes it worked, but I added
CPP / C++ / C Code:
  char *ID.ptr;
	ID_ptr = new int[10];
this was char ID[10]; prior to editting it. I am just confused on how to go about change the program using dynamtic arrays.
  #5  
Old 08-Apr-2009, 02:32
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Need help with dynamic arrays!!


Then you need to explain what's confusing you. Obviously what I said didn't make sense.

Quote:
Originally Posted by missy
yes it worked...
If it worked, didn't the program ... work? What else do you think you need to change?
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #6  
Old 08-Apr-2009, 11:48
missy missy is offline
Awaiting Email Confirmation
 
Join Date: Mar 2009
Posts: 9
missy is on a distinguished road

Re: Need help with dynamic arrays!!


I figure it out. I was confused about the difference between an array and a dynamic array. Thank you for the explaination! I got everything working properly.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error in execution of 2D dynamic array bobsta C++ Forum 4 19-Feb-2009 11:50
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 17:19
Dynamic vs Static Arrays WaltP Miscellaneous Programming Forum 5 16-Feb-2006 16:54
Dynamic Arrays swayp C++ Forum 2 27-Jan-2005 01:24

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:04.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.