Vous êtes sur la page 1sur 6

CASE STUDIES

1. Processing of Examination Marks


Marks obtained by a batch of students in the Annual Examination are tabulated as follows:
Student name
S.Laxmi
V.S.Rao
--

Marks obtained
45 67 38 55
77 89 56 69
- - - -

It is required to compute the total marks obtained by each student and print the rank list based on
the total marks.
The program in Fig.11.14 stores the student names in the array name and the marks in the array
marks. After computing the total marks obtained by all the students, the program prepares and
prints the rank list. The declaration
int

marks[STUDENTS][SUBJECTS+1];

defines marks as a pointer to the array's first row. We use rowptr as the pointer to the row of
marks. The rowptr is initialized as follows:
int (*rowptr)[SUBJECTS+1] = array;
Note that array is the formal argument whose values are replaced by the values of the actual
argument marks. The parentheses around *rowptr makes the rowptr as a pointer to an array of
SUBJECTS+1 integers. Remember, the statement
int

*rowptr[SUBJECTS+1];

would declare rowptr as an array of SUBJECTS+1 elements.


When we increment the rowptr (by rowptr+1), the incrementing is done in units of the size of
each row of array, making rowptr point to the next row. Since rowptr points to a particular row,
(*rowptr)[x] points to the xth element in the row.

POINTERS AND TWO-DIMENSIONAL ARRAYS


Program
#define STUDENTS 5
#define SUBJECTS 4
#include <string.h>
main()

char name[STUDENTS][20];
int marks[STUDENTS][SUBJECTS+1];
printf("Input students names & their marks in four subjects\n");
get_list(name, marks, STUDENTS, SUBJECTS);
get_sum(marks, STUDENTS, SUBJECTS+1);
printf("\n");
print_list(name,marks,STUDENTS,SUBJECTS+1);
get_rank_list(name, marks, STUDENTS, SUBJECTS+1);
printf("\nRanked List\n\n");
print_list(name,marks,STUDENTS,SUBJECTS+1);
}
/*

Input student name and marks

*/

get_list(char *string[ ],
int array [ ] [SUBJECTS +1], int m, int n)
{

int

i, j, (*rowptr)[SUBJECTS+1] = array;

for(i = 0; i < m; i++)


{
scanf("%s", string[i]);
for(j = 0; j < SUBJECTS; j++)
scanf("%d", &(*(rowptr + i))[j]);
}
}
/*

Compute total marks obtained by each student

*/

get_sum(int array [ ] [SUBJECTS +1], int m, int n)


{
int
i, j, (*rowptr)[SUBJECTS+1] = array;

}
/*

for(i = 0; i < m; i++)


{
(*(rowptr + i))[n-1] = 0;
for(j =0; j < n-1; j++)
(*(rowptr + i))[n-1] += (*(rowptr + i))[j];
}

Prepare rank list based on total marks

get_rank_list(char *string [ ],
int array [ ] [SUBJECTS + 1]
int m,
int n)
{
int i, j, k, (*rowptr)[SUBJECTS+1] = array;

*/

char *temp;
for(i = 1; i <= m-1; i++)
for(j = 1; j <= m-i; j++)
if( (*(rowptr + j-1))[n-1] < (*(rowptr + j))[n-1])
{
swap_string(string[j-1], string[j]);

}
/*

for(k = 0; k < n; k++)


swap_int(&(*(rowptr + j-1))[k],&(*(rowptr+j))[k]);
}
Print out the ranked list

*/

print_list(char *string[ ],
int array [] [SUBJECTS + 1],
int m,
int n)
{
int i, j, (*rowptr)[SUBJECTS+1] = array;
for(i = 0; i < m; i++)
{
printf("%-20s", string[i]);
for(j = 0; j < n; j++)
printf("%5d", (*(rowptr + i))[j]);
printf("\n");
}
}
/*

Exchange of integer values

*/

swap_int(int *p, int *q)


{
int temp;
temp = *p;
*p
= *q;
*q
= temp;
}

/*

Exchange of strings

*/

swap_string(char s1[ ], char s2[ ])


{
char swaparea[256];
int
i;
for(i = 0; i < 256; i++)
swaparea[i] = '\0';
i = 0;
while(s1[i] != '\0' && i < 256)

swaparea[i] = s1[i];
i++;

}
i = 0;
while(s2[i] != '\0' && i < 256)
{
s1[i] = s2[i];
s1[++i] = '\0';
}
i = 0;
while(swaparea[i] != '\0')
{
s2[i] = swaparea[i];
s2[++i] = '\0';
}
}
Output
Input students names & their marks in four subjects
S.Laxmi 45 67 38 55
V.S.Rao 77 89 56 69
A.Gupta 66 78 98 45
S.Mani 86 72 0 25
R.Daniel 44 55 66 77
S.Laxmi
V.S.Rao
A.Gupta
S.Mani
R.Daniel

45
77
66
86
44

67
89
78
72
55

38
56
98
0
66

55
69
45
25
77

205
291
287
183
242

77
66
44
45
86

89
78
55
67
72

56
98
66
38
0

69
45
77
55
25

291
287
242
205
183

Ranked List
V.S.Rao
A.Gupta
R.Daniel
S.Laxmi
S.Mani

Fig.11.14 Preparation of the rank list of a class of students.


2. Inventory Updating
The price and quantity of items stocked in a store changes every day. They may either increase
or decrease. The program in Fig.11.15 reads the incremental values of price and quantity and
computes the total value of the items in stock.
The program illustrates the use of structure pointers as function parameters. &item, the address
of the structure item, is passed to the functions update() and mul(). The formal arguments
product and stock, which receive the value of &item, are declared as pointers of type struct
stores.

STRUCTURES AS FUNCTION PARAMETERS


Using structure pointers
Program
struct stores
{
char name[20];
float price;
int
quantity;
};
main()
{
void update(struct stores *, float, int);
float
p_increment, value;
int
q_increment;
struct stores item = {"XYZ", 25.75, 12};
struct stores *ptr = &item;
printf("\nInput increment values:");
printf(" price increment and quantity increment\n");
scanf("%f %d", &p_increment, &q_increment);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - */
update(&item, p_increment, q_increment);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - */
printf("Updated values of item\n\n");
printf("Name
: %s\n",ptr->name);
printf("Price
: %f\n",ptr->price);
printf("Quantity : %d\n",ptr->quantity);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - */
value = mul(&item);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - */
printf("\nValue of the item = %f\n", value);
}

void update(struct stores *product, float p, int q)


{
product->price += p;
product->quantity += q;
}
float mul(struct stores *stock)
{
return(stock->price * stock->quantity);
}

Output
Input increment values: price increment and quantity increment
10 12
Updated values of item
Name
Price
Quantity

: XYZ
: 35.750000
: 24

Value of the item

858.000000

Fig.11.15 Use of structure pointers as function parameters.

Vous aimerez peut-être aussi