-
Help with a project
Hi there, i need a help with writing a program with 3 functions:
void Init (char ***names, int **grades, int *size) that gets The function initializes through input from the user a set of names and a set of scores. Both arrays of the same size, size. Whenever data is collected for arrays, they must be valid. If the user typed an invalid statistic, an error message should be printed and an alternate statistic requested. A valid score is complete between 0 and 100. A valid name meets the following conditions:• Begins with a large Latin letter.
• All but the first characters are lowercase Latin characters.
• Not already in the array. (The array must not contain the same name twice.)
For example:
Enter number of students: 3
Enter name: Ronit
Enter grade: 79
Enter name: yosi
Bad name. Try again: YOSI
Bad name. Try again: Y051
Bad name. Try again: Yosi
Enter grade: 93
Enter name: Ronit
Bad name. Try again: Efrat
Enter grade: 888
Bad grade. Try again: 88
the second function is int Find (char **names, int *grades, int size, char *name) The function accepts, as parameters, an array of names, an array of scores, and the size of these arrays. In addition, it receives as a student name parameter. The function finds the student's position in the set of names, and returns its grade. If the student does not appear in the set, 1- will be returned.
For example, if the arrays names, grades are like the previous example, size = 3, name = ”Efrat”, then the returned value is 88.
the last function is void FreeAll (char ***names, int **grades, int size) The function neatly frees all the memory of the arrays.
THANKS A LOT
-
What sort of help do you need?
Is it everything, or just the finer detail of dealing with three stars and malloc?
Can you for example complete the assignment if you started with
char names[10][100];
int grades[10];
What does your proposed main() look like?
-
Hi mate
the main supposed to use with the help of functions we have created an array that receives a number of student receives it, prints the result and eventually releases all memory.
-
So in a very basic sense, you could do this.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void input( char ***ptr ) {
*ptr = malloc( sizeof(**ptr) );
(*ptr)[0] = malloc( 20 * sizeof(***ptr) );
strcpy((*ptr)[0],"King Arthur");
}
int main ( ) {
char **ptr;
input( &ptr );
printf("Result=%s\n", ptr[0] );
free( ptr[0] );
free( ptr );
}
-
You've got two conditions on the name. Firstly that it be a valid Latin name, secondly that it not be already entered. I would handle these rather different requirements separately.
Start by writing a function that tests if the name is a valid Latin name. Then write another function that tests whether a name is in a list of strings - a list of strings is passed
in as a char ** together with a size variable. Get the first function working, then the second. Then populate your name list from input, and check for duplicates, and put in the
error suppression code. Then you substantially have your init function.
To test it, write another function, called something like "dump_results". This will print out your list of names with associated grades. You don't submit this function, it
is just there to test that init() has been written corectly and is doing what you want it to do.