Struct

Exercise 1

Below is a source code to show 10 data saved in an array. Write the source code and save it as "array1.c". After compilation, confirm by executing it.

#include <stdio.h>
#define N 4

struct student{
    char name[20];
    int eng;
    int math;
    int phys;
};

static struct student data[]={
    {"Jack", 82, 72, 58},
    {"Young", 77, 82, 79}, 
    {"Steeve", 52, 62, 39}, 
    {"Mark", 61, 82, 88}
}; 

int main(void)
{
    int i;

    for(i=0; i<N; i++){
        printf("%7s: Eng = %3d  Math = %3d   Phys = %3d\n", 
        data[i].name, data[i].eng, data[i].math, data[i].phys);
    }

    return (0);

}
(Execution result)
$ ./a.out
   Jack: Eng =  82  Math =  72   Phys =  58
  Young: Eng =  77  Math =  82   Phys =  79
 Steeve: Eng =  52  Math =  62   Phys =  39
   Mark: Eng =  61  Math =  82   Phys =  88
$

Exercise 2

In addition to Exercise 1, we want to calcute mean of three subjects of each person. Write a below source code and save it as "array2.c". After compilation, confirm by executing it.

#include <stdio.h>
#define N 4

struct student{
    char name[20];
    int eng;
    int math;
    int phys;
    double mean;
};

static struct student data[]={
    {"Jack", 82, 72, 58, 0.0},
    {"Young", 77, 82, 79, 0.0}, 
    {"Steeve", 52, 62, 39, 0.0}, 
    {"Mark", 61, 82, 88, 0.0}
}; 

int main(void)
{
    int i, j;

    // Calculation of mean of three subject
    for(i=0; i<N; i++){
        data[i].mean = (data[i].eng + data[i].math + data[i].phys)/3.0;
    }

    for(i=0; i<N; i++){
        printf("%7s: Eng = %3d  Math = %3d   Phys = %3d:  
        Mean = %5.1f\n", data[i].name, data[i].eng, data[i].math, 
        data[i].phys, data[i].mean);
    }

    return (0);

}
(Execution result)
$ ./a.out
   Jack: Eng =  82  Math =  72   Phys =  58: Mean =  70.7
  Young: Eng =  77  Math =  82   Phys =  79: Mean =  79.3
 Steeve: Eng =  52  Math =  62   Phys =  39: Mean =  51.0
   Mark: Eng =  61  Math =  82   Phys =  88: Mean =  77.0
$

Exercise 3

In addition to Exercise 2, we want to determine grade based on mean of three subjects and table shown in below. Write a below source code, complement the expression hidden by "******" and save it as "struct3.c". Confirm by executing it.

MeanGrade
90 <= x <= 100S
80 <= x < 90A
70 <= x < 80B
60 <= x < 70C
x < 60D
#include <stdio.h>
#define N 4

struct student{
    char name[20];
    int eng;
    int math;
    int phys;
    double mean;
    char grade;
};

static struct student data[]={
    {"Jack", 82, 72, 58, 0.0},
    {"Young", 77, 82, 79, 0.0}, 
    {"Steeve", 52, 62, 39, 0.0}, 
    {"Mark", 61, 82, 88, 0.0}
}; 

int main(void)
{
    int i, j;

    // Mean of three subjects and grading based on mean
    for(i=0; i<N; i++){
    
        // Mean of three subjects
        data[i].mean = (data[i].eng + data[i].math + data[i].phys)/3.0;

        // Grading based on the mean
        ****************************************
        ****************************************
        ****************************************
        ****************************************
        ****************************************
        ****************************************
    }

    for(i=0; i<N; i++){
        printf("%7s: Eng = %3d  Math = %3d   Phys = %3d: 
        Mean = %5.1f, Grade = %c\n", data[i].name, data[i].eng, 
        data[i].math, data[i].phys, data[i].mean, data[i].grade);
    }

    return (0);

}
(Execution result)
$ ./a.out
   Jack: Eng =  82  Math =  72   Phys =  58: Mean =  70.7, Grade = B
  Young: Eng =  77  Math =  82   Phys =  79: Mean =  79.3, Grade = B
 Steeve: Eng =  52  Math =  62   Phys =  39: Mean =  51.0, Grade = D
   Mark: Eng =  61  Math =  82   Phys =  88: Mean =  77.0, Grade = B
$

Exercise 4

For the same purpose as Exercise 1, in this exercise, we want to refer to member of struct variable. The differnce between Exercise 1 and Exercise 4 are listed as follows.

Write a below source code and save it as "struct4.c". After compilation, confirm by executing it.

#include <stdio.h>
#define N 4

typedef struct student{
    char name[20];
    int eng;
    int math;
    int phys;
}STUDENT;

STUDENT data[]={
    {"Jack", 82, 72, 58},
    {"Young", 77, 82, 79}, 
    {"Steeve", 52, 62, 39}, 
    {"Mark", 61, 82, 88}
}; 
STUDENT *p;


int main(void)
{
    int i;

    // Pointer variable p refers to the address of data
    p = data;

    for(i=0; i<N; i++){
        printf("%10s: English = %3d  Math = %3d   Physics = %3d\n", 
        p->name, p->eng, p->math, p->phys);

      // Shifting of address
      p++;
    }

    return (0);

}

Exercise 5

We want to calculate set menu price by discounting 10% of total prices of all items. However, less than 10 JPY should be round down. Details on set, item and price are listed in a below table. Write a below source code, complement the expression hidden by "******" and save it as "struct5.c". Confirm by executing it.

SetItems
ABurger, Drink, Potato
BCheese burger, Drink, Potato
CChicken burger, Drink, Potato
D Chicken burger, Drink, Potato, Ice cream 
ItemPrice (JPY)
Burger130
Cheese burger150
Chicken burger200
Drink130
Potato120
Ice cream160
#include <stdio.h>

int main(void)
{
	int i, sum_price=0;

	struct single{
		char	*name;
		int		price;
		struct single	*p;
	};

	struct set{
		char	*set_name;
		int		set_price;
		struct single	*p;
	};

	struct set s[]={{"set A", 0, NULL},
		{"set B", 0, NULL},
		{"set C", 0, NULL},
		{"set D", 0, NULL}};

	struct single burger[] = {{"Burger", 130, NULL},
		{"Cheese burger", 150, NULL},
		{"Chicken burger", 200, NULL}};

	struct single drink = {"Drink", 130, NULL};
	struct single potato = {"Potato", 120, NULL};
	struct single ice = {"Ice cream", 160, NULL};

	struct single *pt;


	// Set B: Cheese burger, Drink, Potato
	s[1].p = &burger[1];
	burger[1].p = &drink;
	drink.p = &potato;


	printf("********** %s ***********\n", s[1].set_name);


	// Calculate set price by 10 % discount of sum of all items
	for(pt = s[1].p; pt != NULL; pt = pt->p){
		sum_price += pt->price;
		printf("%17s: %5d JPY\n", pt->name, pt->price);
	}
	printf("----------------------------\n");
	printf("Sum of all items : %5d JPY\n", sum_price);

	s[1].set_price = (int)(sum_price*0.9)/10*10;
 	printf("       Set price : %5d JPY\n", s[1].set_price);

	return (0);
	
}
(Execution result)
$ ./a.out
********** set B ***********
    Cheese burger:   150 JPY
            Drink:   130 JPY
           Potato:   120 JPY
----------------------------
Sum of all items :   400 JPY
       Set price :   360 JPY
$

Exercise 6

In Exercise 5, we defined only price of set B. We want to define other set prices in the same manner. Program to show below execution results and save the source code as "struct6.c". Confirm whether it can execute properly.

(Execution result)
$ ./a.out
Select one among following set menus
 1: set A (Burger, Drink, Potato)
 2: set B (Cheese burger, Drink, Potato)
 3: set C (Chicken burger, Drink, Potato)
 4: set D (Chicken burger, Drink, Potato, Ice cream)
1
********** set A ***********
           Burger:   130 JPY
            Drink:   130 JPY
           Potato:   120 JPY
----------------------------
Sum of all items :   380 JPY
       Set price :   340 JPY
$
$ ./a.out
Select one among following set menus
 1: set A (Burger, Drink, Potato)
 2: set B (Cheese burger, Drink, Potato)
 3: set C (Chicken burger, Drink, Potato)
 4: set D (Chicken burger, Drink, Potato, Ice cream)
2
********** set B ***********
    Cheese burger:   150 JPY
            Drink:   130 JPY
           Potato:   120 JPY
----------------------------
Sum of all items :   400 JPY
       Set price :   360 JPY
$
$ ./a.out
Select one among following set menus
 1: set A (Burger, Drink, Potato)
 2: set B (Cheese burger, Drink, Potato)
 3: set C (Chicken burger, Drink, Potato)
 4: set D (Chicken burger, Drink, Potato, Ice cream)
3
********** set C ***********
   Chicken burger:   200 JPY
            Drink:   130 JPY
           Potato:   120 JPY
----------------------------
Sum of all items :   450 JPY
       Set price :   400 JPY
$
$ ./a.out
Select one among following set menus
 1: set A (Burger, Drink, Potato)
 2: set B (Cheese burger, Drink, Potato)
 3: set C (Chicken burger, Drink, Potato)
 4: set D (Chicken burger, Drink, Potato, Ice cream)
4
********** set D ***********
   Chicken burger:   200 JPY
            Drink:   130 JPY
           Potato:   120 JPY
        Ice cream:   160 JPY
----------------------------
Sum of all items :   610 JPY
       Set price :   540 JPY
$

Lessons for C programming
Junichi Susaki, Kyoto University