TAILIEUCHUNG - Bài giảng Tin học đại cương A (dành cho khối tự nhiên): Array

Bài giảng sau đây trình bày các kiến thức cơ bản về mảng trong ngôn ngữ lập trình C. Đây là kiến thức thuộc Tin học đại cương dành cho khối tự nhiên. Qua bài giảng bạn sẽ học được khái niệm, cách sử dụng và các ví dụ liên quan. Mời bạn đọc theo dõi. | Array Lecturer: Nguyen Dung Faculty of Information Technology Definition • An array is a finite set of variable of the same data type • Exampe Index 0 1 2 3 4 5 6 7 8 a 3 5 4 3 8 7 9 12 34 2 How to use • Declares: – The first method: [number_nember]; – The second method, etc, : self_study • Access: [index] • Example: int a[10]; a[0] = 3; a[1] = 7; int b = (a[0]*3 + a[1])/4; 3 Examples • For an array is declared as follow: int temperature[7]; • Write a program to input 7 integer numbers into an array above. • Determine the maximum value entered and show the index number for the maximum value in array above. 4 Solve void main(){ int temperature[7]; int maximum, index; for(int i = 0; i < 7; i++){ printf(“Temperature[%d]”, i); scanf(“%d”, &temperature[i]); } maximum = temperature[0]; index = 0; for(int i = 0; i < 7; i++) if (temperature[i] > maximum){ maximum = temperature[i]; index = i; } printf(“The maximum value is: %d”, maximum); printf(“This is number %d in the list of number”, %d); getch(); } 5

TỪ KHÓA LIÊN QUAN
Đã 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.