Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Yes, i know it's homework

Name: Anonymous 2005-11-18 22:25

But i still wanna ask you guys

here's the problem

/////////////////////////////////////////////////////////////////////
// Program 10                  POINTERS                  COSC 1436
/////////////////////////////////////////////////////////////////////

      Define a record structure to hold a set of test scores,
      number of tests, and the average score. The number of
      records are to be determined at runtime. Also the
      number of tests for each record is to be determined at
      runtime. Calculate the average test score for the set
      the set of test scores in each record. Display records.
      Demonstrate with a least five records.


This question must use pointers (couldn't do to class that day, car problem)
i want to ask, is there a way to create a array in the structure, with the size of the array to be defined later?
like

struct record
{
    int test[];
    int num_test;
    float average;
};

and ya, it didn't compile, where am i wrong?

Name: Anonymous 2005-11-19 0:18

either the array should have a number in it or the array should be a pointer to an array. you want the second version. so...int* test;

Name: Anonymous 2005-11-19 0:55

vector<int> scores; // ftw

Name: Anonymous 2005-11-19 4:42

not using C FTW

Name: Anonymous 2005-11-19 9:29

>>1

My first impulse is to say >>3 ftw, but then I saw the title of your assignment was "pointers" and realized that your instructor doesn't want you to use the STL.  (Which is sad, but understandable if you don't know how to se pointers.)  Rather, you're being taught how to allocate and deallocate memory on the heap.

So, first of all, C or C++?  If you're using C, use malloc et al.  If you're using C++, use new et al.

Second, whether your instructor wants it or not, your record "structure" would probably be more useful, conceptually, as a class with intelligent construction and destruction semantics.

So:

(haven't tried to use code tags here before, see if the idiot that I am can get it right)

class TestScoreSet {
public:
   TestScore();
   ~TestScore();
   // Inspectors/Mutators, as appropriate

private:
   int* pScores;
   int  cntScores;
   double meanScore;
};

Your constructor or accessor methods should alloc up some memory on the heap and (if appropriate) free old memory.  Likewise, your destructor should free up any memory still alloc'd.  In your accessors, you'll also want to automatically re-calc your mean and score count so that it's always up to date.

Of course, I think that a more intelligent way to structure this would be to create a test score class or struct (note singular, not plural), create a container of that (like a vector), and wrap the container with this "set" class adding on the averaging logic.  Dangit, I still think >>3's on the money.

Name: Anonymous 2005-11-19 11:45

thx guys, here's the solution
#include <iostream>
#include <iomanip>
using namespace std;

struct record_structure
{
    int* test;
    int num_test;
    float average;
};

void main()
{

    //Ask num of records
    int rec_no;
    cout << "Number of records: ";
    cin >> rec_no;

    record_structure* record = new record_structure[rec_no];

    for(int i=0; i < rec_no; i++)
    {
        //ask for number of tests
        cout << "\n   Record: " << i+1 << endl;
        cout << "No. of tests: ";
        cin >> record[i].num_test;


        //loop for getting test scores
        int max_test;

        record[i].test = new int[record[i].num_test];

        int total=0; //total score, for average

        for(int j=0; j<record[i].num_test; j++)
        {
            cout << "Test " << j+1 << " Score: ";
            cin >> record[i].test[j];
            total+=record[i].test[j];
        }

        //calc average
        record[i].average = total / record[i].num_test;
    }


    //display
    for(i=0; i < rec_no; i++)
    {
        cout << "\n\nRec Num " << i+1 << endl << "Num of tests: " << record[i].num_test << endl;
        for(int j=0; j < record[i].num_test; j++)
        {
            cout << "Test " << j+1 << ": " << record[i].test[j] << endl;
        }
        cout << "Average: " << record[i].average << endl;
    }
}

it might look ugly, but atleast it's done
and >>5

where not into classes yet, not even vector, just plain pointers XD

thanks anyway

Name: Anonymous 2008-07-24 23:29

T ::= test
         | what

Name: Anonymous 2008-07-24 23:31

S ::= Failed attempt at granting ``M'' meme status.
M ::= S
      | How do you like the ``<not a meme>'' meme?

Name: Anonymous 2009-02-15 10:13

>
> > test
>
>

test

>
test

Name: Anonymous 2009-02-15 10:24

&gt; &gt;

test

> > test

Name: Anonymous 2009-02-15 10:26


>
>
> test

Name: Anonymous 2009-03-02 23:54

NESTEDTAG

http://4chan.org

http://4ch[/spoiler]an.org

http://4chan.org

http://4ch
an.org

http://4chan.org))))

Name: Anonymous 2009-03-02 23:54

NESTEDTAG

http://4chan.org

http://4ch
an.org

http://4chan.org

http://4ch
an.org

http://4chan.org))))

Name: Anonymous 2009-03-03 2:59

NESTEDTAG

http://4chan.org

http://4ch[/spoiler]an.org

http://4chan.org

http://4ch
an.org

http://4chan.org

http://4chan.org))))

mailto:derp@shitbag.fag

Name: Anonymous 2009-03-03 13:38

A
T
H

NESTEDTAG

http://4chan.org

http://4ch[/spoiler]an.org

http://4chan.org

http://4ch
an.org

http://4chan.org

http://4chan.org

http://4chan.org))))

mailto:derp@shitbag.fag

Name: Anonymous 2009-03-03 13:39

>
>
sdf

Name: Anonymous 2009-03-03 13:39

NO WAY

Name: Anonymous 2009-03-03 13:40

>
YES

Name: Anonymous 2009-03-03 13:41

NESTEDTAG

http://4ch[spoiler]an.org

http://4chan.org

http://4chan.org

http://4ch
an.org

http://4chan.org

http://4chan.org

http://4chan.org))))

mailto:derp@shitbag.fag

Name: Anonymous 2009-03-04 9:43

NESTEDTAG

http://4chan.org

http://4ch[/spoiler]an.org

http://4chan.org

http://4ch
an.org

http://4chan.org

http://4chan.org

http://4chan.org

http://4chan.org))))

mailto:derp@shitbag.fag

Name: Anonymous 2009-03-04 12:12

NESTEDTAG

http://4chan.org

http://4ch[/spoiler]an.org

http://4chan.org

http://4ch
an.org

http://4chan.org

http://4chan.org

http://4chan.org

http://dis.4chan.org/~derp.pl/J,,,,'''-E+_SUS!&$CHRI:0123456789ST.php?action=derp;&;derp))))

mailto:derp@shitbag.fag

http://derp<span>.com

Name: Anonymous 2009-03-04 14:08

http://4ch[spoiler]an.org

http://4chan.org

http://4ch
an.org

Name: Anonymous 2009-03-04 15:19

NESTEDTAG

http://4chan.org

http://4ch[/spoiler]a[spoiler]n.org

http://4chan.org

http://4ch
an.org

http://4chan.org

http://4chan.org

http://4chan.org

http://4chan.org))))

mailto:derp@shitbag.fag nor mal> ,.?)([]{}\|"';:!@#$%^&*<

FIRSTSECONDTHIRD

http://4ch[spoiler]an.org

http://4chan.org

http://4ch
an.org

http://4chan.org

Name: Anonymous 2010-11-28 16:43

Name: !xU.4H3efqI 2011-07-17 17:26

:(

Name: Anonymous 2011-07-20 22:15

jkjkjkjlkñjk

Don't change these.
Name: Email:
Entire Thread Thread List