IN C PROGRAMMING Problem Set – Structure and pointer In this problem set, we wil
ID: 3874503 • Letter: I
Question
IN C PROGRAMMING
Problem Set – Structure and pointer
In this problem set, we will learn about array of structure variables along with the following subjects:
1. structure - struct
2. declaration of array of structure and typedef struct
3. using rand()
4. pointer, pointer arithmetic in a for loop,
5. pass by reference vs. pass by value
6. passing and using a pointer as a function return value of the structure
7. not using magic numbers
8. using #if and #endif in c programming
9. development folder structure
10. compile with user-defined include files and link with user-defined libraries
*Structure is used to store set of values about an object/entity. We sometime want to store multiple such structure variables for hundreds or objects then Array of Structure is used.*
Declaration of Structure
There are a few different styles of the structure declaration.
Style 1:
struct Student {
char name[64];
int age;
};
struct Student students[100];
Style 2:
struct Student {
char name[64];
int age;
} students[100];
Style 3:
typedef struct {
char name[64];
int age;
} Student;
Student students[100];
Step 1 – 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().
Explanation / Answer
Solution to given problem is provided below. Sample execution output is provided for reference.
File: student.h
#ifndef STUDENT_H
#define STUDENT_H
typedef struct {
char name[64];
int age;
} Student;
typedef Student *pStudent;
#endif
File: students.c
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "student.h"
void generateName(char *name)
{
const int MAX_NAMELENGTH = 10;
int nameLength = 1 + (rand() % MAX_NAMELENGTH);
const char charset[] = "abcdefghijklmnopqrstuvwxyz";
for (int index = 0; index < nameLength; index++)
{
int key = rand() % (sizeof(charset) - 1);
name[index] = charset[key];
}
name[0] = toupper(name[0]);
name[nameLength] = '';
}
void generateAge(int *age)
{
const int MAX_AGE = 100;
*age = 1 + (rand() % MAX_AGE);
}
void readStructure(pStudent list, int *nStudents)
{
pStudent student;
printf("Enter number of students: ");
scanf("%d", nStudents);
list = (Student *) malloc(*nStudents * sizeof(Student));
student = list;
srand(time(NULL));
for (int index = 0; index < *nStudents; index++)
{
generateName(student->name);
generateAge(&(student->age));
student = student + 1;
}
}
pStudent printStructure(pStudent list, int nStudents)
{
pStudent student = list;
int minAge = 0;
int minNameLength = 0;
int maxAge = 0;
int maxNameLength = 0;
int age;
int nameLength;
for (int index = 0; index < nStudents; index++)
{
nameLength = strlen(student->name);
if (index == 0)
{
minAge = student->age;
minNameLength = nameLength;
maxAge = student->age;
maxNameLength = nameLength;
}
else
{
if (student->age < minAge) minAge = student->age;
if (student->age > maxAge) maxAge = student->age;
if (nameLength < minNameLength) minNameLength = nameLength;
if (nameLength > maxNameLength) maxNameLength = nameLength;
}
student = student + 1;
}
printf("Name length: min(%d), max(%d) ", minNameLength, maxNameLength);
printf("Age: min(%d), max(%d) ", minAge, maxAge);
return list;
}
int main()
{
int nStudents;
pStudent list;
pStudent student;
printf("Enter number of students: ");
scanf("%d", &nStudents);
list = (Student *) malloc(nStudents * sizeof(Student));
student = list;
srand(time(NULL));
for (int index = 0; index < nStudents; index++)
{
generateName(student->name);
generateAge(&(student->age));
student = student + 1;
}
pStudent retStudent = printStructure(list, nStudents);
for (int index = 0; index < nStudents; index++)
{
printf("(%d) %s, %d ", index, retStudent->name, retStudent->age);
retStudent = retStudent + 1;
}
return 0;
}
Sample Execution Output:
$ gcc students.c
$ ./a.out
Enter number of students: 100
Name length: min(1), max(10)
Age: min(2), max(99)
(0) Rvzdtuyvgu, 4
(1) Z, 4
(2) L, 97
(3) Ustdym, 81
(4) Gvuamnvlqp, 78
(5) Kln, 62
(6) Maqzzrtrmy, 71
(7) Nlfi, 74
(8) Uschltmd, 99
(9) Qfjeg, 26
(10) Hsyafxsgmg, 6
(11) Nuqiwxt, 55
(12) Yynrfxvn, 63
(13) Utzvyw, 68
(14) Ivrej, 16
(15) Rjvmdjk, 40
(16) Bjv, 22
(17) W, 89
(18) Pz, 37
(19) Ydvibo, 10
(20) Blbkhpqqb, 98
(21) Fckebgesv, 92
(22) Mdlhm, 35
(23) Zy, 33
(24) Bit, 3
(25) Lsu, 81
(26) Wkdxqh, 68
(27) Mb, 28
(28) Pgc, 30
(29) Dczpdjlw, 91
(30) Qew, 14
(31) Gucybvlpx, 15
(32) Mslpvq, 40
(33) Gxgrvqplw, 93
(34) Yuwat, 56
(35) Gqvtyhl, 42
(36) Hcq, 88
(37) Plbkdsxze, 26
(38) Bxaww, 21
(39) Otvmahazq, 43
(40) Vmehpqkh, 20
(41) Ook, 16
(42) Mn, 14
(43) Pxgn, 21
(44) Wmimozjc, 18
(45) Txdak, 94
(46) Yzfjnsuak, 32
(47) Zdsvpcjfds, 36
(48) Lchofrdvr, 87
(49) Cpxwshnzgt, 16
(50) Ktmsyh, 66
(51) Sgpglgj, 33
(52) Nlbejzws, 95
(53) Bhoeuj, 48
(54) Hcppuveai, 11
(55) Rnycqcnp, 52
(56) E, 39
(57) Nlphwi, 74
(58) Kjx, 57
(59) Egpqrggri, 75
(60) Ymtesrnfe, 67
(61) Bnkjztggck, 7
(62) Dixjzhfthr, 25
(63) Lgar, 77
(64) Inuswv, 9
(65) Bq, 54
(66) Hsa, 47
(67) Zphvxalk, 49
(68) Nfgunv, 75
(69) Sjvxnlmf, 13
(70) Fcigupdr, 61
(71) Dcmq, 48
(72) Mwqad, 21
(73) Zhzmvgjcn, 16
(74) Vf, 31
(75) Yssewewh, 40
(76) Dpljzx, 17
(77) Ywf, 47
(78) Kt, 30
(79) Pazqzuifso, 54
(80) Nmdey, 33
(81) Xynyvt, 7
(82) Davcpwbhx, 28
(83) Eqff, 59
(84) Svzs, 39
(85) Pi, 21
(86) Fnui, 9
(87) Fuldy, 49
(88) Tlgjq, 49
(89) Ngxp, 87
(90) Uprofxez, 94
(91) Totgsryum, 64
(92) Ybpb, 72
(93) Yexhamzqty, 21
(94) Htnvotn, 22
(95) Ktcn, 2
(96) Cvvbvby, 74
(97) Metgeoyljm, 11
(98) Hwebizdv, 22
(99) Zsganj, 95