Question

In: Computer Science

Exercise 1: Write a C program that does the following tasks: a. Declare a structure called...

Exercise 1: Write a C program that does the following tasks:

a. Declare a structure called StudRec with four components:

an int containing the StuId,

a string containing the StudName,

a string containing the MajorName,

b. Define typedef List to be a synonym for the type struct StudRec.

c. Declare a global variable array StudST[] of List.

d. Declare a global variable Ptr to be a pointer to List.

e. Write a C function (return pointer to List) that does the following. It accepts the StudST array and an N integer denoting the actual size of the array, read and fill N student details using structure, Dynamic Memory Allocation from the keyboard and return a pointer of List.

f. Declare a structure called Major with two components:

a string containing the MajorName,

an int NumSt containing the number of students in the Major,

g. Declare a global variable array MajorST[] of structure Major.

h. Write a C function that does the following. It accepts the MajorST array with 4 integer denoting the actual size of the array and update the array with 4 MajorName read from the keyboard and assign 0 to NumSt.

i. Write a C function that does the following. It accepts, as arguments, the StudST array (e) with N integer denoting the actual size of the array and the MajorST array (h). Using StudST, the function calculates the number of students in each major and updates the MajorST array by these numbers for each MajorName.

j. Compile and Run the previous statements in a main C file.

Solutions

Expert Solution

Program

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


/*
Declare a structure called StudRec with four components:
- an int containing the StuId,
- a string containing the StudName,
- a string containing the MajorName,

Define typedef List to be a synonym for the type struct StudRec.
*/
typedef struct StudRec{
   int Stud;
   char StudName[100];
   char MajorName[100];
}List;

// Declare a global variable array StudST[] of List.
List StudST[1000];

//Declare a global variable Ptr to be a pointer to List.
List *Ptr;

/*
function (return pointer to List) that does the following. It accepts the StudST array and an
N integer denoting the actual size of the array, read and fill N student details using structure, Dynamic
Memory Allocation from the keyboard and return a pointer of List.
*/
List* readStudents(List StudST[], int N){
  
   Ptr = (List *)malloc(N*sizeof(List));
   int i;
   for(i=0; i<N; i++){
      printf("Enter the ID of Student: ");
      scanf("%d", &(Ptr+i)->Stud);
      printf("Enter the name of Student: ");
      scanf("%s", (Ptr+i)->StudName);
      printf("Enter the major of Student: ");
      scanf("%s", (Ptr+i)->MajorName);
   }
     
   return Ptr;
}

/*
Declare a structure called Major with two components:
- a string containing the MajorName,
- an int NumSt containing the number of students in the Major,
*/
struct Major{
   char MajorName[100];
   int NumSt;
};

// Declare a global variable array MajorST[] of structure Major.
struct Major MajorST[1000];

/*
function that does the following. It accepts the MajorST array with 4 integer denoting the
actual size of the array and update the array with 4 MajorName read from the keyboard and assign 0 to
NumSt.
*/
void readMajors(struct Major MajorST[4]){
  
   int i=0;
   for(i=0; i<4; i++){
       printf("Enter the name of the Major %d", i+1);
       scanf("%s", MajorST[i].MajorName);
       MajorST[i].NumSt = 0;
   }
  
}

/*
function that does the following. It accepts, as arguments, the StudST array (e) with N integer
denoting the actual size of the array and the MajorST array (h). Using StudST, the function calculates
the number of students in each major and updates the MajorST array by these numbers for each
MajorName.
*/
void updateNumMajors(List StudST[], int N, struct Major MajorST[4]){
   int i=0;
   for(i=0;i<N;i++){
       if(strcmp(StudST[i].MajorName, MajorST[0].MajorName) == 0){
           MajorST[0].NumSt++;
       }
       else if(strcmp(StudST[i].MajorName, MajorST[1].MajorName) == 0){
           MajorST[1].NumSt++;
       }
       else if(strcmp(StudST[i].MajorName, MajorST[2].MajorName) == 0){
           MajorST[2].NumSt++;
       }
       else if(strcmp(StudST[i].MajorName, MajorST[3].MajorName) == 0){
           MajorST[3].NumSt++;
       }
      
       else{
           printf("Something went wrong");
       }
      
   }
}

// printing hello world just to check everything compiles fine
int main(){
printf("Hello World");
return 0;
}

==========================================================================================

Hope this helps!

Please let me know if any changes needed.

Thank you!

I hope you're safe during the pandemic.


Related Solutions

Write a program in c++ to do the following : (1) Declare an array a of...
Write a program in c++ to do the following : (1) Declare an array a of size 10 and three pointer variables p, q, and v. (2) Write a loop to fill array a with values 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 (3) write following statement: p= &a[2]; q = &a[5]; i = *q - *p; cout<<“The value of i is”<< i; i = *p - *q; cout<<“The value of i is %d”<< i; 4) assign...
In C++ ============================================================EXERCISE 2 PROBLEM SET============================================================ 1) Declare a structure named TempScale , with the following...
In C++ ============================================================EXERCISE 2 PROBLEM SET============================================================ 1) Declare a structure named TempScale , with the following members:fahrenheit: a doublecentigrade: a doubleNext, declare a structure named Reading , with the following members:windSpeed: an inthumidity: a doubletemperature: a TempScale str ucture variableNext define a Reading structure variable. ------------------------------------------------------------ 2) Write statements that will store the following data inthe variable you defined in Problem 1:Wind Speed: 37 mphHumidity: 32%Fahrenheit temperature: 32 degreesCentigrade temperature: 0 degrees ------------------------------------------------------------ 3) Write a function called showReading. It...
Write a program in C that does the following: 1. Declares an array called numbers_ary of...
Write a program in C that does the following: 1. Declares an array called numbers_ary of 6 integer numbers. 2. Declares an array called numbers_ary_sq of 6 integer numbers. 3. Reads and sets the values of numbers_ary from the keyboard using a loop. 4. Sets the values of numbers_ary_sq to the square of the values in numbers_ary using a loop. 5. Displays the values of numbers_ary and the values of numbers_ary_sq beside each other using a loop. Example Output Assume...
Write a simple Java program that does the following: 1) Declare a constant of type String...
Write a simple Java program that does the following: 1) Declare a constant of type String to hold the words "Oakland University". 2) Declare variables of the type stated, and Prompt the user to enter in the following information and store in the variables a. Their current GPA on a 4.0 scale, into a variable of type double b. The number of credits they have so far into a variable of type int c. The amount of tuition they paid...
Write a program that does the following in C++ 1 ) Write the following store data...
Write a program that does the following in C++ 1 ) Write the following store data to a file (should be in main) DC Tourism Expenses 100.20 Revenue 200.50 Maryland Tourism Expenses 150.33 Revenue 210.33 Virginia Tourism Expenses 140.00 Revenue 230.00 2 ) Print the following heading: (should be in heading function) Store name | Profit [Note: use setw to make sure all your columns line up properly] 3 ) Read the store data for one store (should be in...
C++ Vectors. Create a program do the following in the program: 1. declare an vector without...
C++ Vectors. Create a program do the following in the program: 1. declare an vector without specifying the size 2. use push_back to add random integers between 100 and 999 to the vector 3. write a function that returns the smallest, largest, and average of the numbers in the vector display the smallest, largest, and average of the numbers in the vector
C++ Code Write a program that performs the following: a) Declare long variables value1 and value2....
C++ Code Write a program that performs the following: a) Declare long variables value1 and value2. The variable value1 has been initialized to 200000. Declare the variable longPtr to be a pointer to an object of type long. b) Assign the address of variable value1 to pointer variable longPtr. c) Display the value of the object pointed to by longPtr. d) Assign the value of the object pointed to by longPtr to variable value2. e) Display the value of value2....
Write a program (in Q0.c) to do the following: In main(), declare an integer x. Print...
Write a program (in Q0.c) to do the following: In main(), declare an integer x. Print the address of x (using the address-of operator). Pass x as an argument to a function void fooA(int* iptr). (Hint: can you pass x itself directly?) In fooA(int* iptr), print the value of the integer pointed to by iptr, the address pointed to by iptr, and the address of iptr itself. In the main function, following the call to fooA(...) , print the value...
Write a program to perform the following two tasks: 1. The program will accept a string...
Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and smell the rose". Display the result string. 2. Then...
Write a program to perform the following two tasks: 1. The program will accept a string...
Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and smell the rose". Display the result string. 2. Then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT