# student marks and array , i have got this far , any pointers?

Page 1 of 1

## 14 Replies - 39469 Views - Last Post: 02 May 2011 - 04:53 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=230062&amp;s=107f23cc8fb7c9c5edda06719a673141&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 gruffy

• D.I.C Head

Reputation: 4
• Posts: 106
• Joined: 28-April 11

# student marks and array , i have got this far , any pointers?

Posted 30 April 2011 - 07:08 PM

hi all and thanks for reading , any help is much appreciated ...
PROBLEM DEFINITION:
Write a program to input 20 student marks into an array
and then calculate and display:
the student marks
the average grade,
the highest grade
and the lowest grade.

I have written this so far and but my knowledge is patchy at best as am still a beginner, i was wondering if i could have a bit of help on what needs to be added to the below code in order to get the marks stored in the array and establish input and output for it, thanks ahead , if any takers etc , and thanks for reading this
Yours humble-y , gruffy

file enclosed below for download:
stud_marks.c.txt (674bytes)
Number of downloads: 1010

```#include <stdio.h>
//#define MAX_ARRAY 10
#define S_MAX_ARRAY 10
#define M_MAX_ARRAY 3

int main () {

char student[S_MAX_ARRAY];
int mark[M_MAX_ARRAY];
int low = 100;
int high = 0;
int average = 0;

for(int i = 0; i<S_MAX_ARRAY;++i)
{
printf("please enter student name");
scanf(" %s",&student[i]);
}

for(int i = 0; i<S_MAX_ARRAY;++i)
{
if(mark[i] > high) high = mark[i];

if(mark[i] < low) low = mark[i];

average+= mark[i];

}

average/=M_MAX_ARRAY;
for(int i = 0; i<M_MAX_ARRAY;++i)
{
printf("Mark %d = %d \n", i,mark[i]);
}
printf("Average %d \n", average);
printf("high = %d \n", high);
printf("low = %d \n", low);

return 0;
}

```

as mentioned thanks for reading this
:):):):):):)::):)
Gruffy

ps i think i am asking to return memory adresses and not values to , im not too sure , but dont know how to fix either way

This post has been edited by gruffy: 30 April 2011 - 07:10 PM

Is This A Good Question/Topic? 0

## Replies To: student marks and array , i have got this far , any pointers?

### #2 jimblumberg

Reputation: 5443
• Posts: 16,968
• Joined: 25-December 09

## Re: student marks and array , i have got this far , any pointers?

Posted 30 April 2011 - 07:15 PM

First do you need to enter 20 student marks or 20 students? You are entering 20 students but you are never entering any marks.

Also you need to find an indentation style and use it. This will make your program much easier to read and troubleshoot.

Jim
Was This Post Helpful? 1

### #3 gruffy

• D.I.C Head

Reputation: 4
• Posts: 106
• Joined: 28-April 11

## Re: student marks and array , i have got this far , any pointers?

Posted 30 April 2011 - 08:10 PM

jimblumberg, on 30 April 2011 - 07:15 PM, said:

First do you need to enter 20 student marks or 20 students? You are entering 20 students but you are never entering any marks.

Also you need to find an indentation style and use it. This will make your program much easier to read and troubleshoot.

Jim

sorry jim your absolutely right, for some reason i copied it into notepad 1st then into here and lost identation ?, dunno , you tell me , am a bit tired so not most aware at mo.
below is improved indents
....and to answer your question:

The program must allow the user to enter a student name and a score mark for 20 students i total,then display the student names and marks and underneath or at the top display the average of all the marks, the highest mark and the lowest mark. <(i like him, he just keeps on going )... anyway thanks for such a quick response.
.....as mentioned the revised indednted code below:
```#include <stdio.h>
//#define MAX_ARRAY 10
#define S_MAX_ARRAY 10
#define M_MAX_ARRAY 3

int main () {

char student[S_MAX_ARRAY];
int mark[M_MAX_ARRAY];
int low = 100;
int high = 0;
int average = 0;

for(int i = 0; i<S_MAX_ARRAY;++i)
{
printf("please enter student name");
scanf(" %s",&student[i]);
}

for(int i = 0; i<S_MAX_ARRAY;++i)
{
if(mark[i] > high) high = mark[i];

if(mark[i] < low) low = mark[i];

average+= mark[i];

}
average/=M_MAX_ARRAY;

for(int i = 0; i<M_MAX_ARRAY;++i)
{
printf("Mark %d = %d \n", i,mark[i]);
}
printf("Average %d \n", average);
printf("high = %d \n", high);
printf("low = %d \n", low);

return 0;
}

```

once again cheers Jim

This post has been edited by gruffy: 30 April 2011 - 08:18 PM

Was This Post Helpful? 0

### #4 jimblumberg

Reputation: 5443
• Posts: 16,968
• Joined: 25-December 09

## Re: student marks and array , i have got this far , any pointers?

Posted 30 April 2011 - 08:26 PM

So if you need to enter 20 students with how many marks per student? You might want to consider using a structure to group the student name with the student marks.

Jim
Was This Post Helpful? 1

### #5 gruffy

• D.I.C Head

Reputation: 4
• Posts: 106
• Joined: 28-April 11

## Re: student marks and array , i have got this far , any pointers?

Posted 01 May 2011 - 04:42 AM

thanks jim
could you let me know if the below will
instantiate the struct correctly:

```struct stud_marks{

char student[20];
int mark[20];
}

//i think i call it like this.....
int main()
{
printf("please enter a student mark...");
scanf(&student[0]);
printf("please enter a score.....");
scanf(&mark);
}
```

thank you so much for your help, so far Jim, you and many others have been a great help to my understanding of C over the past few days (its like a crash course for me, due to duff lecturing)
*(i know that sounds like an excuse but he really hasnt helped the situation by trying to relate alot of the task related assignment to our knowledge surrounding Pascal- i get this sneaky feeling he may actually have never done C or portable mid level languages before and may be blagging it a bit(tis true he has a lot of experience in things like fortan,and cobol) but surely it would be better to have knowledge of subject matter you are going to tutor/lecture etc .. )*

anyway thanks again for help , tis life saving
Gruffy

This post has been edited by gruffy: 01 May 2011 - 04:45 AM

Was This Post Helpful? 0

### #6 jimblumberg

Reputation: 5443
• Posts: 16,968
• Joined: 25-December 09

## Re: student marks and array , i have got this far , any pointers?

Posted 01 May 2011 - 05:37 AM

First you are missing a semicolon on the end of your structure definition. And you must then declare an instance of your structure inside main.

```#define MAX_STUDENTS 3

struct stud_marks{
char student[20];
int mark[20];
};

int main()
{
struct stud_marks students[MAX_STUDENTS]

return 0;
}

```

The above code defines and declares the structure stud_marks. Notice since you need to enter multiple students that I created an array to hold this information. This structure has a C-string variable named student which will allow a single single student name of 20 characters long, and allow for a maximum of 20 marks per student.

Please see this link for structures in C. It should explain how to define and use structures in your program. Also see this link for scanf and printf.

Do not forget to add the proper include files when you use functions like scanf() and printf(), stdio.h.

Jim
Was This Post Helpful? 1

### #7 gruffy

• D.I.C Head

Reputation: 4
• Posts: 106
• Joined: 28-April 11

## Re: student marks and array , i have got this far , any pointers?

Posted 01 May 2011 - 06:08 AM

jimblumberg, on 01 May 2011 - 05:37 AM, said:

First you are missing a semicolon on the end of your structure definition. And you must then declare an instance of your structure inside main.

Thanks and on way not to look up about sturcures from your link, thanks and thanks and thanks again Jim

Quote

This structure has a C-string variable named student which will allow a single single student name of 20 characters long, and allow for a maximum of 20 marks per student.

Jim

My array is supposed to store one student mark per student -
i.e studentname:Gareth //char value array
studentmark:50 //integer value array,
perhaps linked, in 3d arrray (or would it be 2d)
thanks for being patient with me
Gruffy
Was This Post Helpful? 0

### #8 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: student marks and array , i have got this far , any pointers?

Posted 01 May 2011 - 06:20 AM

Are you allowed to use structs?
If you are then the 'linkage' you dicuss will be handled by the struct.

For your purposes just change the example that jimblumberg gave you to remove the marks array and replace it with a marks int variable inside the struct.

Then create an array of 20 of those structs Instead of the 3 jimblumberg gave you.
Was This Post Helpful? 1

### #9 gruffy

• D.I.C Head

Reputation: 4
• Posts: 106
• Joined: 28-April 11

## Re: student marks and array , i have got this far , any pointers?

Posted 01 May 2011 - 07:01 AM

```#include <stdio.h>
#include <stdlib.h>
#define MAX_STUDENTS 20

struct stud_marks{
char student[15];
int mark;
};

int main()
{
struct stud_marks students[MAX_STUDENTS];
int i, j;

for ( i=0; i<20; i++)
for (j=0; j<20; j++)
do{
printf("please enter student mark");
scanf("%s",&stud_marks students);
}while (i <=20);

return 0;
}

```

I have started this , is this gonnna work?
i dunno,
Thanks for tips though so far (nod to jim, i know i need to go over the printf scanf link, and kind of get that actually scanf can be unpredictable regarding nulls and return keyboard strokes, (if i remember it correctly etc) and that fscanf ii the safer mode of approach please correct me if im worng )

my main issue is how to , again , instantiate this syntactically, cos im newbie an all that jazziness
Cheers for reading all.
Gruffy
Was This Post Helpful? 0

### #10 jimblumberg

Reputation: 5443
• Posts: 16,968
• Joined: 25-December 09

## Re: student marks and array , i have got this far , any pointers?

Posted 01 May 2011 - 08:15 AM

You are not accessing the structure elements correctly.

Compare how you are trying to access the elements to the example from the link I posted above:

Quote

WHAT IS A STRUCTURE?

struct1.c

```#include

struct
{
char initial;     /* last name initial      */
int  age;         /* childs age             */
int  grade;       /* childs grade in school */
} boy, girl;

int main()
{
boy.initial = 'R';  // This is how you access an element of a structure.
boy.age = 15;
boy.grade = 75;

```

You really should take another look at this link. Pay attention to the numerous examples in this link.

Also you are not using scanf correctly so please study the links I posted.
The links are scanf and printf.

If you have questions about the material in the links, ask.

Jim
Was This Post Helpful? 1

### #11 gruffy

• D.I.C Head

Reputation: 4
• Posts: 106
• Joined: 28-April 11

## Re: student marks and array , i have got this far , any pointers?

Posted 01 May 2011 - 08:40 AM

Quote

You really should take another look at this link. Pay attention to the numerous examples in this link.

Also you are not using scanf correctly so please study the links I posted.
The links are scanf and printf.

If you have questions about the material in the links, ask.

Jim

thanks Jim , getting on it now , will pst back asap , buttrying to bring to gether what i have fo rmy porject as running oout of time to complete, (the links have been excellent , but it is true i skimmed over them and not read thoroughly, but for above reason, have saved links for future and further referencing etc)
They , i can see, will be valable in my journey through C
Thanks again matey, will post something back by tonight
thanks all contributors
Gruffy
Was This Post Helpful? 0

### #12 gruffy

• D.I.C Head

Reputation: 4
• Posts: 106
• Joined: 28-April 11

## Re: student marks and array , i have got this far , any pointers?

Posted 01 May 2011 - 05:45 PM

```#include <stdlib.h>                                         // std include header std library
#include <stdio.h>                                          // std include header std input/output
#include <windows.h>                                     // std include header for Windows specifics
#include <string.h>	/* strcat */
#define STRINGLEN 20
/*begin function*/
//define student array stucture
struct student{
char studentname[15];                 //variable char array[15] - stud_name
int studentgrade;                      //variable float - stud_score
}student;                                    //declare student array

void gotoxy(int x, int y) {
char essq[1000];		/* String variable to hold the escape sequence */
char xstr[1000];		/* Strings to hold the x and y coordinates */
char ystr[1000];
/* Escape sequences must be built with characters */

/*
** Convert the screen coordinates to strings
*/
sprintf(xstr, "%f", x);
sprintf(ystr, "%f", y);

/*
** Build the escape sequence (vertical move)
*/
essq[0] = '\0';
strcat(essq, "\033[");
strcat(essq, ystr);

/*
** Described in man terminfo as vpa=\E[%p1%dd
** Vertical position absolute
*/
strcat(essq, "f");

/*
** Horizontal move
** Horizontal position absolute
*/
strcat(essq, "\033[");
strcat(essq, xstr);
/* Described in man terminfo as hpa=\E[%p1%dG */
strcat(essq, "G");

/*
** Execute the escape sequence
** This will move the cursor to x, y
*/
//printf("%s", essq);
}

//start main
int main(int argc, char *argv[])
{

struct student studentarray[20];// standard array of structs for student data

char choice;   //general choice var sey as char incase of user error input
int studentpos = 0;  //Student array position var
float average = 0;   //Average store
float min = 100;     //Minimun num store
float max = 0;       //Maximum num store
float totalmarks = 0;//Total marks for average

do{
system("cls"); //C doesnt have a cls, so use dos com
fflush(stdin);

for(studentpos = 0; studentpos < 4; studentpos++)//loop to get user vals
{
//get student details and fill stucture
printf("Please input Student name %d - >", studentpos + 1);
//input for student name max length of 20
scanf("%s",&studentarray[studentpos].studentname);

printf("\nPlease input Student grade - > ");
scanf("%2d", &studentarray[studentpos].studentgrade);
fflush(stdin);
system("cls");
}//for end

system("cls"); // clear screen

printf("Student grade statistics\n\n");

//get the average
for(studentpos = 0; studentpos < 4; studentpos++)//loop to get user vals
{

totalmarks += studentarray[studentpos].studentgrade;//calculate the average

//Max set to 0 and compared. Stores higher num
if(studentarray[studentpos].studentgrade > max)
{
max = studentarray[studentpos].studentgrade;//add if the number is higher
}//1 end if

//Min set to 100 and compared. Store low num
if(studentarray[studentpos].studentgrade < min)
{
min = studentarray[studentpos].studentgrade;//add if the number is higher
}//2 end if
}//for end
//start calculating
average = (totalmarks / 20); //get the average
studentpos = 0;                                 //print the detail of inputs

printf("Student name ------------- Student grade\n");
for(studentpos = 0; studentpos < 4; studentpos++){
gotoxy(0,studentpos + 4);
printf("%s ", studentarray[studentpos].studentname);
gotoxy(27, studentpos + 4);
printf("%f\n ", studentarray[studentpos].studentgrade);
}

//print calculations
gotoxy(0, 13);
printf("Average grade = %.2f \n ", average);
printf("The highest grade = %.2f \n ", max);
printf("The lowest grade = %.2f \n ", min);

printf("Do you want to enter some more values...E or e to re-input - > ");
fflush(stdin);
scanf("%c", &choice);

}while(choice == 'e' || choice == 'E'); //do end

printf("\nGoodbye \n");

system("PAUSE");
return 0;
}

```

any reasn yu guys know of as to why the gotoxy function, which i have addedto allow non dos exclusive cursor control. ie unix systems should be able to implement this and get the same result as you get with gotoxy -

DECLARED as below, when using microsoft console/dos/command prompt type environment

FOR MS-DOS
```void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

```

thanks for any help so far , and just tell if im becoming a burden to your time
- still lovin that banana man

could it be the way im calling to the goto function, i have just assumed it is the same as calling to the dos function would be cos is effectively replaced but same name etc.
sorry for being a newb, will sort my stupid head out after this week over , he says... thanks again jimand janotte

btw here is the link for the webpage that supplied the gotoxy code :
c/C++ tutorials snippet

i think ive replied in the wrong box
going mental
Gruffy
Was This Post Helpful? 0

### #13 jimblumberg

Reputation: 5443
• Posts: 16,968
• Joined: 25-December 09

## Re: student marks and array , i have got this far , any pointers?

Posted 01 May 2011 - 07:15 PM

I don't understand your question.

However you still have some warnings that you need to take care of.

Quote

main.c|15|warning: no previous declaration for ‘gotoxy’|
main.c||In function ‘gotoxy’:|
main.c|24|warning: format ‘%f’ expects type ‘double’, but argument 3 has type ‘int’|
main.c|25|warning: format ‘%f’ expects type ‘double’, but argument 3 has type ‘int’|
main.c||In function ‘main’:|
main.c|79|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[15]’|
main.c|118|warning: format ‘%f’ expects type ‘double’, but argument 2 has type ‘int’|
main.c|57|warning: unused parameter ‘argc’|
main.c|57|warning: unused parameter ‘argv’|
||=== Build finished: 0 errors, 7 warnings ===|

What compiler IDE and operating system are you using?

Please check the compiler settings and insure warnings are being emitted. This is very important. It is almost impossible to write reliable code without the compiler emitting warnings. Errors alone are not enough.

Jim
Was This Post Helpful? 0

### #14 gruffy

• D.I.C Head

Reputation: 4
• Posts: 106
• Joined: 28-April 11

## Re: student marks and array , i have got this far , any pointers?

Posted 02 May 2011 - 01:27 AM

thank for advice , jim .
my issue is with ms dos using the cls function , making this code viable to compile on a dos machine and execute fine, but does not conform to portablibity rules cos there is no ms dos calls on linux based etc machines.
..i think this is true anyways....
i have commented out the dos gotoxy function, and replaced with what (i found) to be an alternative ... my issue was that it doesnt implement the same way , and doesnt carry through what i have askd it to do.

was wondering if there was something i have nt done ,put code in wrong place etc etc .

thanks for the kind replies so far jim,and i will get theose errors sorted, just gotta have a shower , to ensure they still call me human back here on my real world side of this portal, where i have a family and life etc etc (well i once had a family life , thats gone now for sure (thanks C and all your delights !)).
Anyway, if none of the above makes sense , i will be back on after oblutions , sorry if any has further confused you JIM etc
Was This Post Helpful? 0

### #15 gruffy

• D.I.C Head

Reputation: 4
• Posts: 106
• Joined: 28-April 11

## Re: student marks and array , i have got this far , any pointers?

Posted 02 May 2011 - 04:53 AM

jimblumberg, on 01 May 2011 - 07:15 PM, said:

I don't understand your question.

What compiler IDE and operating system are you using? -

Quote

i am using windows and codeblocks(gnu gcc minGW c99)(gruffy)

Please check the compiler settings and insure warnings are being emitted. This is very important. It is almost impossible to write reliable code without the compiler emitting warnings. Errors alone are not enough.

Jim

using windows (7 pro) and i am just interested to see if this can be made truly portable as a cursor function in a console environment.... linux does not have one, according to the code comments of the bit of code to create a cursor function etc
i know the gotoxy function taken from MSDOS (specific to windows only (crrect me if wrong)) but not sure how to completely instantiate the portable code posted above, anyway thanks for taking the time to read, i will endevour and will get those warnings gone i promise, but want to get this code sorted to add in adn have functioning correctly too. Thaknks again JIM (super great help as is now becoming the usual, sorry about this your free to state this about me, )
gruffy
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }