Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me to do my C programming. Step1 - Warming-up Implement a program in a function which gets three student names and ages and

Please help me to do my C programming.

Step1 - Warming-up

Implement a program in a function which gets three student names and ages and store in the structure and print them one by one.

- Ask the user to enter a name and his/her age

- Use Student array of structure

- Use NSTUDENTS

For example: (0) John, 20 (1) Han, 30 (2) Dong, 40

Step 2 No magic number NSTUDENTS

In this step, we want to remove the magic number NSTUDENTS. Now you must ask the user to enter the number of students and allocate the memory dynamically for the array of structure to store students' name and age.

- Allocate a pointer to an array structure instead of using a fixed size array.

- Don't use NSTUDENTS, but use nStudents which user entered.

- Use the array notation [ ] to access members of the structure.

For example: (0) Axp, 29 (1) Kbpstep 88 (2) Dntomqcey 9

Step 3 Using pointer -> notation to access members of a structure

In this step, it is the same as step 2 except you use a pointer to access members of the structure instead of the array notation.

- Don't use the array notation [], but use -> to access members

- Don't forget incrementing the pointer in for loop

Step 4 Using typedef and Generating names and ages randomly

In this step, it is the same as step 3 except you generate names and ages randomly instead of asking them to users.

- Use typedef Student *pStudent instead of Student *

For example, use pStudent list; don't use Student *list

To use this typedef structure, you must turn off the struct declaration and turn on typedef struct on in the source file provided.

///////////////////////////// typedef struct ///////////////////////////// typedef struct { char name[BUFFER_SIZE]; int age; } Student; typedef Student *pStudent;

- Each name begins with a capital letter and its length must be one or more but less than

MAX_NAMELENGTH = 10. Implement this functionality in void

generateName(char *name)

- The age must be between 0 and MAX_AGE = 100. Implement this functionality in

void generateAge(int *age)

For example: (0) Axp, 29 (1) Kbpstep 88 (2) Dntomqcey 9

Step 5 implementing printStructure()

In this step, it is the same as step 4 except you use printStructure().

- Implement a function called printStructure() which takes two arguments as shown below: printStructure(pStudent list, int nStudents) printStruecture() prints the contents of structure and its information about the min/max names' length and ages.

Step 6 Passing and getting a pointer

In this step, it is the same as Step 5, except it does not print but returns the pointer of the student list such that the caller of the function prints if necessary.

- In main() function, print the list of students, not in step6() function.

- Don't forget freeing the pointer in main().

These are the codes to follow.

/** * Description: This program is written to learn the following subjects: * 1. structure - struct * 2. array of structure * 3. typedef struct * 4. using malloc(), free(), malloc, rand() * 5. pointer, pointer arithmetic in a for loop, pass by reference * 6. passing and using a pointer as a function return value * 7. not using magic numbers * 8. using #if and #endif in c programming * * */

#ifdef DEBUG #define DPRINT(func) func; #else #define DPRINT(func) ; #endif

#include #include #include #include

#include "nowic.h"

#define BUFFER_SIZE 64 #define MAX_NAMELENGTH 10 #define MAX_AGE 100 #define NSTUDENTS 3

typedef struct { char name[BUFFER_SIZE]; int age; } Student;

typedef Student *pStudent;

// This function is to be implemented for Step 4, 5, and 6. // Generate a string that begins with an uppercase letter // len is one more than the name length to include NULL char. // name[BUFFER_SIZE] should be large enough to store 'len' characters + 1 void generateName(char *name) { int i = 0; int len = rand() % MAX_NAMELENGTH + 2; // len = number of char + 1

static const char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; static const char lower[] = "abcdefghijklmnopqrstuvwxyz";

name[i] = upper[rand() % (sizeof(upper) - 1)]; // setting 1st capital letter

printf("your code here "); }

// This function is to be implemented for Step 4, 5, and 6. // The age is set to [0..MAX_AGE] inclusively. // *age - using pass by reference void generateAge(int *age) { printf("your code here "); }

// This function is to be implemented for Step 5. void printStructure(pStudent list, int n) { int maxNameLen = -1, minNameLen = MAX_NAMELENGTH; int maxAge = -1, minAge = 1000;

for (int i = 0; i < n; i++, list++) { printf("\t(%d) %s, \t\t%d ", i, list->name, list->age);

printf("your code here "); } printf("\tName length(min,max):(%d,%d), Age(min,max)=(%d,%d) ", minNameLen, maxNameLen, minAge, maxAge); }

// Test a program which uses "Student and NSTUDENTS" // Ask the user to enter a name and his / her age // Use Student array of structure and NSTUDENTS void step1() { Student list[NSTUDENTS];

for (int i = 0; i < NSTUDENTS; i++) { printf(" \tEnter a name(%d/%d): ", i + 1, NSTUDENTS); strcpy(list[i].name, GetString()); printf("your code here "); }

// use for loop to print the student list printf("your code here ");

}

// Get a name and an age for nStudents. // - Allocate a pointer to an array structure instead of using a fixed size array. // - Don't use NSTUDENTS, but use nStudents which user entered. // - Use the array notation [] to access members void step2(int nStudents) {

printf("your code here "); // use malloc() and check memory using assert()

// free the pointer used here

}

// Generate names and ages randomly // - Don't use typedef Student *pStudent. // - Don't use the array notation [], but use -> to access members // - Don't forget incrementing the pointer in for loop and freeing memory void step3(int nStudents) { printf("your code here "); // use malloc() to allocate an array structure to a pointer, check the memory alloc

// save the pointer to use later.

// use for/while loop and update a pointer one by one

// use for loop to print the student list // recover the original pointer to print the list from the first

// free the pointer used here

}

// Generate names and ages randomly for nStudents // - Use typedef Student *pStudent. // - For example, use pStudent list; don't use Student *list void step4(int nStudents) { printf("your code here ");

// use for loop to print the student list; use -> notation

// recover the original pointer to print the list from the first

}

// Generate names and ages randomly for nStudents // - Copy step4() and rename it as step5(). // - Implement a function called printStructure() which takes two arguments // as shown below: // printStructure(pStudent list, int nStudents)

void step5(int nStudents) {

printf("your code here ");

// use printStructure();

}

// Generate names and ages randomly for nStudents // - Copy step5() and rename it as step6() // - Change the function return type to the pointer of the students list. // - Remove the code which print the list, // - Return the pointer to main() pStudent step6(int nStudents) { printf("your code here ");

return NULL; }

int main(int argc, char *argv[]) { int option; int nStudents; pStudent list; // for Step 6

// Use setvbuf() to prevent the output from buffered on console. setvbuf(stdout, NULL, _IONBF, 0);

do { printf(" \tTesting Options " "\t1 - step 1: Using a fixed size array of structure " "\t2 - step 2 Using a 'malloc'ed array of structure " "\t3 - step 3: Using a 'malloc'ed array and pointers " "\t4 - step 4: Using pointers, random string stuff " "\t5 - step 5: Using typedef Student *pStudent " "\t6 - step 6: Printing student list in main() " "\tEnter an option(0 to quit): ");

option = GetInt(); if (option >=2 && option <= 6) { printf(" \tEnter a number of students: "); nStudents = GetInt(); if (nStudents == 0) continue; } switch (option) { case 1: step1(); break; case 2: step2(nStudents); break;

case 3: step3(nStudents); break;

case 4: step4(nStudents); break;

case 5: step5(nStudents); break;

case 6: // invoke step6() and get the pointer returned to print. list = step6(nStudents);

// use printStructure() to print the list.

// free the pointer

break;

default: break; } } while (option != 0);

// system("pause"); return EXIT_SUCCESS;

}

Thank you so much:)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions