Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,094 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,592 people online right now. Registration is fast and FREE... Join Now!




how to set array size in the output console? not in the coding.

 
Reply to this topicStart new topic

how to set array size in the output console? not in the coding., need help

rhys
1 Oct, 2007 - 09:58 AM
Post #1

New D.I.C Head
*

Joined: 1 Oct, 2007
Posts: 5


My Contributions
#include<iostream>
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: How To Set Array Size In The Output Console? Not In The Coding.
1 Oct, 2007 - 10:01 AM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,440



Thanked: 64 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(rhys @ 1 Oct, 2007 - 10:58 AM) *

#include<iostream>

I'm not sure that I understand the question...?

Maybe you could define a MAX value, set your array based on an input value, & if there isn't one, set via the MAX (fall back).


User is online!Profile CardPM
+Quote Post

rhys
RE: How To Set Array Size In The Output Console? Not In The Coding.
1 Oct, 2007 - 10:23 AM
Post #3

New D.I.C Head
*

Joined: 1 Oct, 2007
Posts: 5


My Contributions
QUOTE(rhys @ 1 Oct, 2007 - 10:58 AM) *

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

void main;
{
int noofstudent[999]



QUOTE(rhys @ 1 Oct, 2007 - 11:19 AM) *

QUOTE(rhys @ 1 Oct, 2007 - 10:58 AM) *

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

void main;
{
int noofstudent[999]

cout>>"enter numbers of students : ";
cin<<noofstudent;

then, let say i have 10 students, how can i put it this way..

enter number of students : 10
enter marks : 85
45
65
87
62
75
62
48
95
24
the highest score is : 95



User is offlineProfile CardPM
+Quote Post

no2pencil
RE: How To Set Array Size In The Output Console? Not In The Coding.
1 Oct, 2007 - 10:32 AM
Post #4

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,440



Thanked: 64 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
I'm not sure 100% if this would work (or if it's what you want)

CODE

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

#define MAX 256

int main main(int argc,char *argv[]) {  
  int noofstudent=0;
  if(argc > 1) {
        noofstudent=argv[1];
  }

  return 0;
}


Depending on what the variable noofstudent is going to referance, it doesn't sound to me like it should be an array. But you'll need to explain more on what you are trying to accomplish.

Maybe you want to setup a struct, defined with a max of noofstudent variable that was input. Then you could use the struct to contain multiple variables of different variable types. Age (int), sex(char), grade (char or int), class (char)... etc.

QUOTE

CODE

void main;
{
int noofstudent[999]



Fyi, this contains a few errors that will halt your program from the get-go.

1.) main should be an int return type
2.) the ; after declaring main, will halt the program.
3.) you need to have parens () after main for any input type sent to main. If none, then use void.
4.) you are missing ; after your int. ; is needed at the end of any single statement.
User is online!Profile CardPM
+Quote Post

rhys
RE: How To Set Array Size In The Output Console? Not In The Coding.
1 Oct, 2007 - 10:41 AM
Post #5

New D.I.C Head
*

Joined: 1 Oct, 2007
Posts: 5


My Contributions
[quote name='no2pencil' date='1 Oct, 2007 - 11:32 AM' post='262178']
I'm not sure 100% if this would work (or if it's what you want)

CODE
#include<iostream>
#include<algorithm>
using namespace std;

#define MAX 256

int main main(int argc,char *argv[]) {  
  int noofstudent=0;
  if(argc > 1) {
        noofstudent=argv[1];

cout>>"enter numbers of students : ";
cin<<noofstudent;
cout>>"enter scores : ";
cin<<??
  }

  return 0;
}


the program will prompt the user numbers of student, then with respected value (20, input 20 scores. 10 n 10 scores) and finally it will display the highest score.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: How To Set Array Size In The Output Console? Not In The Coding.
1 Oct, 2007 - 11:08 AM
Post #6

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,440



Thanked: 64 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(rhys @ 1 Oct, 2007 - 11:41 AM) *

the program will prompt the user numbers of student, then with respected value (20, input 20 scores. 10 n 10 scores) and finally it will display the highest score.

If you want a program that prompts, then you do not want to use :
int main main(int argc,char *argv[])

CODE

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  int noofstudent=0,i=0;
  int grades[256];

  printf("Enter the number of students :");
  scanf("%d",&noofstudent);
  if(noofstudent>256) {
    printf("You have entered too many students.\n");
    exit(0);
  }

  for(i=0;i<noofstudent;i++) {
    printf("Please enter grade for Student #%d :",i);
    scanf("%d",&grades[i]);
  }

  for(i=0;i<noofstudent;i++) {
    printf("Student #%d : %d\n",i,grades[i]);
  }

  return 0;
}



Will produce something like this
QUOTE

Enter the number of students :3
Please enter grade for Student #0 :95
Please enter grade for Student #1 :90
Please enter grade for Student #2 :82
Student #0 : 95
Student #1 : 90
Student #2 : 82


Good luck on the rest.
User is online!Profile CardPM
+Quote Post

rhys
RE: How To Set Array Size In The Output Console? Not In The Coding.
1 Oct, 2007 - 09:28 PM
Post #7

New D.I.C Head
*

Joined: 1 Oct, 2007
Posts: 5


My Contributions
#include <iostream>
using namespace std;
void main()
{
int noofstudent = 0, i = 0;
int grades[256];

cout>>"Enter the number of students :";
cin<<"%d",&noofstudent);
}

for(i=0;i<noofstudent;i++) {
cout >> "Please enter grade for Student #%d :",i;
cin << "%d",&grades[i];
}

for(i=0;i<noofstudent;i++) {
cout >> "Student #%d : %d\n",i,grades[i];
}

return 0;
}

User is offlineProfile CardPM
+Quote Post

rhys
RE: How To Set Array Size In The Output Console? Not In The Coding.
1 Oct, 2007 - 11:57 PM
Post #8

New D.I.C Head
*

Joined: 1 Oct, 2007
Posts: 5


My Contributions
#include <iostream>
using namespace std;
void main()
{
int noofstudent = 0, i = 0;
int grades[256];

cout>>"Enter the number of students :";
cin<<"%d",&noofstudent);
}

for(i=0;i<noofstudent;i++) {
cout >> "Please enter grade for Student #%d :",i;
cin << "%d",&grades[i];
}

for(i=0;i<noofstudent;i++) {
cout >> "Student #%d : %d\n",i,grades[i];
}

return 0;
}

then how to display highest grade?
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: How To Set Array Size In The Output Console? Not In The Coding.
2 Oct, 2007 - 02:51 AM
Post #9

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,440



Thanked: 64 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(rhys @ 2 Oct, 2007 - 12:57 AM) *

then how to display highest grade?

Use an if inside of your loop..... show some effort =-)


QUOTE(rhys @ 2 Oct, 2007 - 12:57 AM) *

CODE

#include <iostream>
using namespace std;
void main()  // <-- Void
{
int noofstudent = 0, i = 0;
int grades[256];

...

return 0;   // <-- Int
}


This code is wrong. You cannot declare main as type void & then issue an int return type. Perhaps you should re-read over your subject material before continuing on. I think you are trying to get through the project without understanding it. You will be setting yourself up for failure if you do not understand what you are doing now, in the beginning stages.
User is online!Profile CardPM
+Quote Post

Smarf
RE: How To Set Array Size In The Output Console? Not In The Coding.
2 Oct, 2007 - 06:44 AM
Post #10

D.I.C Head
**

Joined: 21 Sep, 2007
Posts: 80



Thanked: 2 times
My Contributions
I would like to suggest the following, but I'm not sure if this is C++ or C that we're talking about:

CODE

Ask for number of students, and store it in the variable numStudents

THEN

initialize an array using numStudents as the parameter

int scoreArray[numStudents];



That's what it sounded like to me, creating a variable sized array based on the input of the user.
User is offlineProfile CardPM
+Quote Post

Ebrog
RE: How To Set Array Size In The Output Console? Not In The Coding.
2 Oct, 2007 - 07:15 AM
Post #11

New D.I.C Head
*

Joined: 2 Oct, 2007
Posts: 3


My Contributions
Smarf, it is my understanding

int scoreArray[numStudents];

is not proper C++ even though some compilers (Dev-C++ for one) allow it.

What you would need is Dynamic Memory
CODE

int * scores;
scores = new int [number_of_students];


BUT
you have to also free up this memory at the end of your use using
CODE

delete scores [];


However, for the original poster Rhys, I would suggest you avoid this method, since your static array declaration is probably fine for your assignment.

In fact, you could avoid making array completely, if all you need is the student with the highest score (and don't need to store other students score). Simply keep checking newly entered score with previous high score.
According to my professor, don't use arrays when you don't need to.

Finally, take my advice with a grain of salt as I am simply paraphrasing today's lecture. smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:25PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month