Question
1. Modify the program so that it will read student names from a data file and store them into unixClass array. Please use studentNames.txt as
1. Modify the program so that it will read student names from a data file and store them into unixClass array. Please use studentNames.txt as your input file.
2. Use rand function to generate a random age, ranging from 17 to 36 for each student in unixClass.
3. Change the constant SIZE to 500. This will be large enough to store all students from the input file. Remember 500 is defined as the original array size. It is not the actual student count. You will need to keep track the student count while getting input from the data file.
The code is below
#include typedef struct { char name[40]; int age; } student; #define SIZE 2 void getData(student[], int); void printData(student[], int); int main() { student unixClass[SIZE]; getData(unixClass, SIZE); printData(unixClass, SIZE); return 0; } void getData(student anyAry[], int anySz) { int i, c; for (i = 0; i < anySz; i++) { printf("Enter a name: "); gets(anyAry[i].name); printf("Enter age: "); scanf("%d", &anyAry[i].age); c=getchar(); } }
void printData(student anyAry[], int anySz) { int i; for (i = 0; i < anySz; i++) { printf("The name of student %d is %s ", i+1, anyAry[i].name); printf("The age of student %d is %d years old ", i+1, anyAry[i].age); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started