C Programming for the Absolute Beginner phần 9

Tham khảo tài liệu 'c programming for the absolute beginner phần 9', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Chapter 10 Dynamic Memory Allocation After studying the preceding program and Figure you can see that realloc is quite useful for expanding contiguous memory while preserving original memory contents. Chapter Program Math Quiz Shown in Figure 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 Using chapterbased concepts to build the Math Quiz. All of the code required to build the Math Quiz game is demonstrated next. include include include main int response int answer int op1 int op2 char result int x C Programming for the Absolute Beginner Second Edition 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 Chapter 10 Dynamic Memory Allocation 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 .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.