I take one look at this project, define my structure.....and my brain goes numb. I don't know where to star.....

///////project///////
Write a program that dynamically allocates an array large enough to hold a user defined number of structures (string name and three double tests). Using a function, the user will enter a name and three test scores for each element in the array. Using another function, the user will sort the array into ascending order by name. Using a third function a table will be displayed on the screen (under a header labeling each column) showing:

Name Test 1 Test 2 Test 3 Average

Then average for each element (three test grades / 3) will be calculated by a fourth function as it is being displayed.

help of any kind is greatly appreciated

``````#include <iostream>

{
char name;
double test1;
double test2;
double test3;
};

using namspace std;

int main()
{

system("pause")
return 0;
}``````
5
Contributors
12
Replies
14
Views
9 Years
Discussion Span
Last Post by Absaar

If your brain is going numb, it probably means you're overwhelmed with everything that needs to be done. Break the problem down into multiple smaller problems, then solve each one individually. The requirements are pretty specific about what the result will look like (three functions, dynamic allocation, blah blah blah).

If your brain is going numb, it probably means you're overwhelmed with everything that needs to be done. Break the problem down into multiple smaller problems, then solve each one individually. The requirements are pretty specific about what the result will look like (three functions, dynamic allocation, blah blah blah).

well i had a similar assignment in my college .i cannot give you the complete program but the underneath can give u the nudge your looking for.

``````#include<iostream>
using namespace std;

struct student
{
char name[10];
int m1,m2,m3;
float avg;

void findavg()
{
avg = (m1+m2+m3)/3;
}

void disp ()
{
cout<<name<<"\t";
cout<<m1<<m2<<m3<<endl;
}

void accpt()
{
cin>>name>>m1>>m2>>m3;
}
};

int main()
{
int x;
cout<<" enter how many records ?";
cin>>x;
student std[x];
// i guess from here you can write an iterative loop to accept and display the data
// although u will have to write a sorting funnction (outside the structure )to sort the name and print accordingly .

return 0;
}``````

Dear rahul, your code won't compile: `student std[x];` is never going to work :P (edit:: unless x is a constant expression)

Thats just what the OP was asking for: Dynamic Memory allocation, your code doesn't explain dynamic memory allocation I think ...

Edit:: Take a look at this to get a basic idea about dynamic memory, you can easily adapt it for structures :)

ohh sorry for that ....
i will post the corrected one.......

well i just compiled this and it worked...

``````#include<iostream>
using namespace std;

struct student
{
char name[10];
int m1,m2,m3;
float avg;

void findavg()
{
avg = (m1+m2+m3)/3;
}

void disp ()
{
cout<<name;
cout<<m1<<m2<<m3<<endl;
}

void accpt()
{
cin>>name>>m1>>m2>>m3;
}
};

int main()
{
int x ;
cout<<"enter how many records";
cin>>x;
student std[x];
for(int i=0;i<x;i++)
std[i].accpt();
for(int i=0;i<x;i++)
std[i].disp();

return 0;
}``````

well i just compiled this and it worked...

LOL, that's impossible !
What ancient crappy old compiler are you using?

A standard C++ compiler (this is a compiler which complies to the ANSI/ISO C++ standard will never compile this)

The evidence: I compiled it using the Borland Compiler this is the output:

``````Borland C++ 5.82 for Win32 Copyright (c) 1993, 2005 Borland
brol.cpp:
Error E2313 brol.cpp 32: Constant expression required in function main()
*** 1 errors in Compile ***``````

>LOL, that's impossible !
Not really.

>What ancient crappy old compiler are you using?
One modern enough to understand namespaces and the standard library.

>A standard C++ compiler (this is a compiler which complies
>to the ANSI/ISO C++ standard will never compile this)
Never say never. Allowing non-constant array sizes is a relatively common extension.

well i have compiled it and its giving me the output.....

seriously this compiler issue is freaking me out .its not the first time such a thing is happened in this forum

i work on code blocks and i think its mingw compiler.

well i have compiled it and its giving me the output.....

i work on code blocks and i think its mingw compiler.

Sorry, I was wrong about that, but I didn't take into account you were using compiler extensions ...

And Narue has proven me wrong again :P !!

Edit:: I checked it and using the MinGW compiler it compiled perfectly :)

thank you for your help. I have found a sorting function from an example we completed in class!

The functions you used are clearing things up for me.

For Example
student* std[x];
here you will dynamically allocate structure object using new statement;
std=new student[x];

where x is obviously taken as an input...hope it will help in dynamic allocationm.

For Example
student* std[x];
here you will dynamically allocate structure object using new statement;
std=new student[x];

where x is obviously taken as an input...hope it will help in dynamic allocationm.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.