Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

C Programming for the Absolute Beginner phần 9 pot
MIỄN PHÍ
Số trang
33
Kích thước
15.0 MB
Định dạng
PDF
Lượt xem
1702

C Programming for the Absolute Beginner phần 9 pot

Nội dung xem thử

Mô tả chi tiết

After studying the preceding program and Figure 10.5, you can see that realloc() is quite

useful for expanding contiguous memory while preserving original memory contents.

CHAPTER PROGRAM—MATH QUIZ

Shown in Figure 10.6, the Math Quiz game uses memory allocation techniques, such as the

calloc() and free() functions, to build a fun and dynamic quiz that tests the player’s ability

to answer basic addition problems. After studying the Math Quiz program, you can use your

own dynamic memory allocation and random number techniques to build fun quiz programs

of any nature.

FIGURE 10.6

Using chapter￾based concepts to

build the Math

Quiz.

All of the code required to build the Math Quiz game is demonstrated next.

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

main()

{

int response;

int *answer;

int *op1;

int *op2;

char *result;

int x;

Chapter 10 • Dynamic Memory Allocation 241

srand(time(NULL));

printf("\nMath Quiz\n\n");

printf("Enter # of problems: ");

scanf("%d", &response);

/* Based on the number of questions the user wishes to take,

allocate enough memory to hold question data. */

op1 = (int *) calloc(response, sizeof(int));

op2 = (int *) calloc(response, sizeof(int));

answer = (int *) calloc(response, sizeof(int));

result = (char *) calloc(response, sizeof(char));

if ( op1 == NULL || op2 == NULL || answer == NULL || result == NULL ) {

printf("\nOut of Memory!\n");

return;

} // end if

//display random addition problems

for ( x = 0; x < response; x++ ) {

op1[x] = rand() % 100;

op2[x] = rand() % 100;

printf("\n%d + %d = ", op1[x], op2[x]);

scanf("%d", &answer[x]);

if ( answer[x] == op1[x] + op2[x] )

result[x] = 'c';

else

242 C Programming for the Absolute Beginner, Second Edition

result[x] = 'i';

} // end for loop

printf("\nQuiz Results\n");

printf("\nQuestion\tYour Answer\tCorrect\n");

//print the results of the quiz

for ( x = 0; x < response; x++ ) {

if ( result[x] == 'c' )

printf("%d + %d\t\t%d\t\tYes\n", op1[x], op2[x], answer[x]);

else

printf("%d + %d\t\t%d\t\tNo\n", op1[x], op2[x], answer[x]);

} //end for loop

//free memory

free(op1);

free(op2);

free(answer);

free(result);

} // end main

SUMMARY

• Random access memory (RAM) provides a volatile solution for allocating, storing, and

retrieving data. RAM is considered volatile because of its inability to store data after the

computer loses power (shuts down).

• Another volatile memory storage area called virtual memory is a reserved section of the

hard disk in which the operating system can swap memory segments.

• Virtual memory is not as efficient as random access memory, but it does provide an

impression to the CPU that is has more memory that it really does.

Chapter 10 • Dynamic Memory Allocation 243

Tải ngay đi em, còn do dự, trời tối mất!