Ex 09 PDF

Embed Size (px)

Citation preview

  • 8/8/2019 Ex 09 PDF

    1/12

    Text-only version

    This is Google's cache ofhttp://www.hackforums.net/printthread.php?tid=200662. It is a snapshot of the

    page as it appeared on Nov 9, 2010 06:11:56 GMT. The current page could have changed in the

    meantime. Learn more

    These search terms are highlighted:

    afterthearrayshavebeendynamicallyallocated

    Need help dynamicallt allocating a structure!! - Printable Version

    +- Hack Forums (http://www.hackforums.net)

    +-- Forum: Programming, Coding, and Languages (/forumdisplay.php?fid=151)

    +--- Forum: C/C++/Obj-C Programming (/forumdisplay.php?fid=117)

    +--- Thread: Need help dynamicallt allocating a structure!! (/showthread.php?tid=200662)

    Need help dynamicallt allocating a structure!! - kl2eativ - 12-08-200902:40 PM

    Okay i have my structures, And i need to ask the user how many students, and how many

    scores so I can then dynamically allocate the array. But i just cannot find the code for this..

    Would I be using the "new" ?? Please help.

    Code:// Exercise 10 Course grade.#include #include using namespace std;

    const int Size = 25; //Array size

    struct Class{

    char studName[Size]; // Student namedouble quizzes; // Number of quizesdouble midTerm; // Midterm test scoredouble final; //Final test scoredouble finalScore; // Final scoredouble finalGrade; //Course grade

    };

    int main(){

    int grades, numstuds;

    //Get the number of studentscout > numstuds;

    Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5

    of 12 December11 2:38

  • 8/8/2019 Ex 09 PDF

    2/12

    //Get the number of quizzescout > grades

    //Dynamically allocate memory

    }

    * - Etheryte - 12-08-200903:36 PM

    As far as I understand he's looking for the C++ equivalent for the following (this is in reg C).

    Code:#include #include

    int main(){int a, *b;printf("Enter the amount of ... : ");scanf("%d",&a);b=(int*)malloc(a*sizeof(int));return 0;

    }

    * - closed_my_HF_account - 12-08-200903:40 PM

    (12-08-2009 03:36 PM)Etheryte Wrote: As far as I understand he's looking for the

    C++ equivalent for the following (this is in reg C).

    Code:#include #include

    int main(){

    int a, *b;printf("Enter the amount of ... : ");scanf("%d",&a);b=(int*)malloc(a*sizeof(int));return 0;

    }

    If that's the case then he could just use that and simply change scanf to std::cin

    * - g4143 - 12-08-200905:17 PM

    Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5

    of 12 December11 2:38

  • 8/8/2019 Ex 09 PDF

    3/12

    Is this what your looking for: Note I'm not a C++'er so you might want to verify this

    Code:#include

    struct Class{

    char studName[20]; // Student namedouble quizzes; // Number of quizesdouble midTerm; // Midterm test scoredouble final; //Final test scoredouble finalScore; // Final scoredouble finalGrade; //Course grade

    };

    int main(){

    struct Class *cptr = new struct Class[10];//allocate memory for 10 structures

    delete [] cptr;return 0;}

    * - closed_my_HF_account - 12-08-200907:41 PM

    (12-08-2009 05:17 PM)g4143 Wrote: Is this what your looking for: Note I'm not a

    C++'er so you might want to verify this

    Code:

    Seem's OK to me, oh kl2eativ you might want to think about changing the struct name because

    Class is so easily confused with the C++ class keyword, just change it to student_classroom or

    something. I've slightly editted below so you can see the memory allocation for varification in

    the struc (with changed name):

    Code:#include #include

    struct student_classroom{

    char studName[20]; // Student namedouble quizzes; // Number of quizesdouble midTerm; // Midterm test scoredouble final; //Final test scoredouble finalScore; // Final scoredouble finalGrade; //Course grade

    Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5

    of 12 December11 2:38

  • 8/8/2019 Ex 09 PDF

    4/12

    };

    int main(){

    struct student_classroom *cptr = new struct student_classroom[10];//allocatememory for 10 structures

    //Get the number of studentsfor(int i = 0; i < 10; i++){std::cout

  • 8/8/2019 Ex 09 PDF

    5/12

    int main(){

    struct student_classroom *cptr = new structstudent_classroom[10];//allocate memory for 10 structures

    //Get the number of studentsfor(int i = 0; i < 10; i++)

    {std::cout

  • 8/8/2019 Ex 09 PDF

    6/12

    {int a;do{

    std::cout a;

    }while(a30);struct Class *cptr = new struct Class[a];return 0;

    }

    * - kl2eativ - 12-09-200901:42 PM

    guys, sorry to keep bothering but this is my last HW of the semester and I really need to do it to

    maintain my B. why the compiler keeps telling me that I is undeclared at the loop??

    Code:// Exercise 10 Course grade.#include #include

    #include using namespace std;

    const int Size = 25; //Array size

    struct student_classroom{

    char studName[Size]; // Student namedouble quizzes[]; // Number of quizesdouble midTerm; // Midterm test scoredouble programmingExercises[]; //Programming exersices scoresdouble final; //Final test scoredouble finalScore; // Final scoredouble finalGrade; //Course grade

    };

    int main(){int studnum, scoresnum;//Ask how many students in the classcout > studnum;

    //Allocate memory depending on number of studentsstruct student_classroom *cptr = new struct student_classroom[studnum];

    //Ask how many scores per studentcout > scoresnum;cout > student_classroom.quizzes[i];

    delete [] cptr;

    Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5

    of 12 December11 2:38

  • 8/8/2019 Ex 09 PDF

    7/12

    std::cout

  • 8/8/2019 Ex 09 PDF

    8/12

    //Ask how many students in the classcout > studnum;

    //Allocate memory depending on number of studentsstruct student_classroom *cptr = new struct student_classroom[studnum];

    //Ask how many scores per student

    cout > scoresnum;//Get the student IDcout > student_classroom.studName[i]//Enter the score for each quizcout > student_classroom.quizzes[i];//Enter score for each programming exercisecout > student_classroom.programmingExercices[i];//Get the score for the midtermcout > student_classroom.midTerm[i]

    delete [] cptr;//Function to calculate the scoresScoresCalc();FinalCalc();

    system ("pause");return 0;}

    //************************//Functions definitions *//************************

    void ScoresCalc(){

    * - oprichniki - 12-09-200905:19 PM

    Well, as a C user I can say that structures don't allow for ambiguous array sizes, unless you

    work with pointers.

    C++ might have changed that rule, but as far as I know your structure has errors because it

    declares arrays without specifying their size.

    * - g4143 - 12-09-200906:47 PM

    Your really display some fundamental problems in your code - Try Googling "Teach yourself C++

    Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5

    of 12 December11 2:38

  • 8/8/2019 Ex 09 PDF

    9/12

    in 21 days" I think its available on the internet..

    Code:// Exercise 10 Course grade.#include #include #include using namespace std;

    #define SIZE 25

    //Function prototypes;void ScoresCalc(double, double);void FinalCalc(int, char);

    struct student_classroom{

    char studName[SIZE]; // Student namedouble quizzes; // Number of quizesdouble midTerm; // Midterm test scoredouble programmingExercises; //Programming exersices scoresdouble final; //Final test scoredouble finalScore; // Final scoredouble finalGrade; //Course grade

    };

    int main(){

    int studnum, scoresnum, i;cout > studnum;

    struct student_classroom *cptr = new struct student_classroom[studnum];

    cout > scoresnum;

    cout > student_classroom.studName[i]cout > student_classroom.quizzes[i];cout > student_classroom.programmingExercices[i];

    cout > student_classroom.midTerm[i]}

    delete [] cptr;

    //*****************************************************************//your deleting your structures(data) before your using it//**************************************************************

    Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5

    of 12 December11 2:38

  • 8/8/2019 Ex 09 PDF

    10/12

    ScoresCalc();FinalCalc();

    system ("pause");//very bad programming practicereturn 0;}

    //************************//Functions definitions *//************************

    void ScoresCalc(){

    * - closed_my_HF_account - 12-09-200907:19 PM

    I agree, when I saw he didn't know how to handle an un-declared varibable compiler error and

    then ask what to do I thought exactly the same thing - I think he should get a book and start

    from page 1.

    * - kl2eativ - 12-11-200908:11 PM

    guys I really need you help on my last assignment. Here are the instructions

    The program should keep a list of scores for a group of students. It should ask the user how

    many scores there are and how many students there are. It should then dynamically allocate an

    array of structures. Each structure's programmingExercises, and quizzes should point to a

    dynamically allocated array that will hold the scores for programmingExercises, and quizzes

    respectively.

    After the arrays have been dynamically allocated, the program should ask for the student

    ID number and all the test scores for each student. The courseGrade should be calculated as

    follows:

    1. programmingExercises = (Sum of all programming exercise scores / total programming

    exercises points) * .75

    2. quizzes = Sum of all quiz scores * .10

    3. MidTerm = MidTerm exam score * .05

    4. final = Final exam score * .10

    5. finalScore = 1 + 2 +3 + 4

    6. finalGrade = finalScore expressed as a letter grade.

    Please give me ideas, I want to have the formulas for my math calculations.

    * - closed_my_HF_account - 12-11-200908:20 PM

    Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5

    0 of 12 December11 2:38

  • 8/8/2019 Ex 09 PDF

    11/12

    (12-11-2009 08:11 PM)kl2eativ Wrote: guys I really need you help on my last

    assignment. Here are the instructions

    The program should keep a list of scores for a group of students. It should ask the

    user how many scores there are and how many students there are. It should then

    dynamically allocate an array of structures. Each structure's programmingExercises,

    and quizzes should point to a dynamically allocated array that will hold the scoresfor programmingExercises, and quizzes respectively.

    After the arrays have been dynamically allocated, the program should ask for

    the student ID number and all the test scores for each student. The courseGrade

    should be calculated as follows:

    1. programmingExercises = (Sum of all programming exercise scores / total

    programming exercises points) * .75

    2. quizzes = Sum of all quiz scores * .10

    3. MidTerm = MidTerm exam score * .05

    4. final = Final exam score * .10

    5. finalScore = 1 + 2 +3 + 46. finalGrade = finalScore expressed as a letter grade.

    Please give me ideas, I want to have the formulas for my math calculations.

    We're not here to do your homework for you, you would have learnt how to do this in your class

    over time otherwize you wouldn't have been given the exersize, therefore, if you can't do this

    yourself then you don't deserve to pass.

    * - kl2eativ - 12-11-200908:23 PM

    (12-11-2009 08:20 PM)Systemerror Wrote: We're not here to do your homework

    for you, you would have learnt how to do this in your class over time otherwize you

    wouldn't have been given the exersize, therefore, if you can't do this yourself then

    you don't deserve to pass.

    Well to tell you the truth, i did not learn a thing with this teacher, he really sucks ass, whatever i

    learn, i learned it online or the book, not through him. Thanks for the help though.

    * - closed_my_HF_account - 12-11-200908:26 PM

    (12-11-2009 08:23 PM)kl2eativ Wrote: Well to tell you the truth, i did not learn a

    thing with this teacher, he really sucks ass, whatever i learn, i learned it online or

    the book, not through him. Thanks for the help though.

    Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5

    1 of 12 December11 2:38

  • 8/8/2019 Ex 09 PDF

    12/12

    You're only given an assignment (in any topic) if you've learnt everything within it that it entails,

    it's more likely that you wasn't paying attention or something along those lines.

    Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5

    2 of 12 December11 2:38