Question

C programming. 1.Create a program that does the following - Creates three pointers, a character pointer...

C programming.

1.Create a program that does the following
- Creates three pointers, a character pointer professor, and two integer pointers student_ids, grades
- Using dynamic memory, use calloc to allocate 256 characters for the professor pointer
- Prompts the professor for their name, and the number of students to mark.
- Stores the professor’s name using the professor pointer and in an integer the number of students to mark.
- Using dynamic memory, use malloc to allocate memory for student_ids and grades to hold the number of students the professor needs to mark.
- The program does not need to do anything else, ensure that you free your memory before terminating.
- You will need to review the malloc, calloc, and sizeof documentation.

2. Building upon the previous questions you will create a marking system for professors at UOIT.
- Structs can be used the same as any other data type in C, instead of having two arrays for the grades and student ids create a struct called grade that contains two integers: student_id and mark.
- Create a function grade_students which takes the following arguments: a pointer to the grade struct called grades, and an integer num_students. The function returns void and does the following:
- Opens the file grades.txt in write mode
- Using the num_students parameter iterates through all of the grade structs pointed to by the grades parameter (remember arrays are pointers, you can treat pointers like arrays).
- For each grade structure adds the mark member of the struct to a variable called sum that holds the sum of all student’s grades.
- For each grade structure write to the file grades.txt the student id and the mark on a single line.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PLEASE REFER BELOW CODE

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

typedef struct grade
{
    int student_id;
    int mark;
}grade;

void grade_students(struct grade *grades, int num_students)
{
    FILE *fptr;
    int i,sum;
    fptr = fopen("grades.txt", "w");

    sum = 0;
    for(i = 0; i < num_students; i++)
    {
        sum += grades[i].mark;
        fprintf(fptr, "%d\t%d\n", grades[i].student_id,grades[i].mark);
    }
    fclose(fptr);
}

int main()
{
    int num_students;
    struct grade *g;
    char *professor;
    int *student_ids;
    int *grades;
    int i;
    professor = (char*)calloc(256, sizeof(char));

    printf("Enter Professor name : ");
    scanf("%[^\n]", professor);
    printf("Enter number of students to mark : ");
    scanf("%d", &num_students);
    // creating two arrays using malloc
    student_ids = (int*)malloc(sizeof(int) * num_students);
    grades = (int*)malloc(sizeof(int) * num_students);

    //b) //allocating array of struct for num of students
    g = (grade*)malloc(sizeof(grade) * num_students);
//asking user input
    for(i = 0; i < num_students; i++)
    {
        printf("Enter id and marks of %d student : ", (i+1));
        scanf("%d %d", &g[i].student_id,&g[i].mark);
    }
    grade_students(g, num_students); //calling function

    return 0;
}


PLEASE REFER BELOW OUTPUT

OUTPUT FILE grades.txt is

1   45
2   48
3   90
4   99

Add a comment
Know the answer?
Add Answer to:
C programming. 1.Create a program that does the following - Creates three pointers, a character pointer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Rewrite the program you submitted for A8 to use pointers, pointer notation and arithmetic, etc. as...

    Rewrite the program you submitted for A8 to use pointers, pointer notation and arithmetic, etc. as follows: If you did not complete A8, you will need to start with those instructions, and then, change your code based on the instructions here The main() function should: Create a fixed or dynamic array, e.g., double grade[SIZE] or double *grade = malloc(...) or calloc(...) Concerning the function call for getData(), pass the address of the array and its size to pointers; depending on...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • C++ Project - Create a memory game in c++ using structs and pointers. For this exercise,...

    C++ Project - Create a memory game in c++ using structs and pointers. For this exercise, you will create a simple version of the Memory Game. You will again be working with multiple functions and arrays. You will be using pointers for your arrays and you must use a struct to store the move and pass it to functions as needed. Program Design You may want to create two different 4x4 arrays. One to store the symbols the player is...

  • C programming The program will require the following structure: struct _data { char *name; long number;...

    C programming The program will require the following structure: struct _data { char *name; long number; }; The program will require command line arguments: int main(int argv, char **argc) { Where argv is the number of arguments and argc is an array holding the arguments (each is a string). Your program must catch any case where no command line arguement was provided and print a warning message (see below). You MUST include/use the following functions, defined as follows: int SCAN(FILE...

  • IN C ONLY As mentioned earlier there are two changes we are going to make from...

    IN C ONLY As mentioned earlier there are two changes we are going to make from lab 5, The file you read into data structures can be any length. studentInfo array will be stored in another struct called studentList that will contain the Student pointer and current length of the list. Sometimes data can be used in structs that correlate between variables so it's convenient to store the data in the same struct. Instead of tracking a length variable all...

  • In C++ Assignment 8 - Test Scores Be sure to read through Chapter 10 before starting this assignment. Your job is to write a program to process test scores for a class. Input Data You will input a tes...

    In C++ Assignment 8 - Test Scores Be sure to read through Chapter 10 before starting this assignment. Your job is to write a program to process test scores for a class. Input Data You will input a test grade (integer value) for each student in a class. Validation Tests are graded on a 100 point scale with a 5 point bonus question. So a valid grade should be 0 through 105, inclusive. Processing Your program should work for any...

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • Create a C program to implement a grade book. Must follow these guidelines: 1. Use #define...

    Create a C program to implement a grade book. Must follow these guidelines: 1. Use #define to define MAX_SIZE1 as 20, and MAX_SIZE2 as 10 2. Use typedef to define the following struct type: struct { char name[MAX_SIZE1]; int scores[MAX_SIZE2]; } 3. The program accepts from the comand line an integer n (<= 30) as the number of students in the class. You may assume that the input will always be valid. 4. Dynamically allocate memory for an array of...

  • C - language please 1. Create a program that does the following, make sure you can...

    C - language please 1. Create a program that does the following, make sure you can complete this before moving to further questions, when compiling add the -Ipthread argument, if you are using gcc use the -pthread argument. Creates two threads, the first uses a function hello world() which prints hello world, the second uses a function goodbye() which prints goodbye. • Each function has a random sleep duration before printing the output - After running your program a few...

  • Write a program in C++ that uses a class template to create a set of items....

    Write a program in C++ that uses a class template to create a set of items. . . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT