코딩/C언어

_11_교재 => 수업012_배열 ~ 수업012_배열03 , _11_문제 => 문제012_배열 ~ 문제012_배열08 (1~3)

tree0505 2025. 7. 1. 10:55
반응형
    • _11_교재

  • 수업012_배열.cpp
#include <stdio.h>

int main(void) {

	int arr[3] = { 10, 20, 30 };
	int i = 0;
	for (i = 0; i < 3; i++) 
		printf("%d\n", arr[i]);
	

	return 0;
}

  • 수업012_배열02.cpp
#include <stdio.h>
int main(void) {
	//c언어는 사이즈가 중요하다. 

	//sizeof() 함수 // 메모리 크기를 반환해준다. 
		//메모리 크기를 알려준다. 

	char ch; //char는 1바이트 
	char chList[5]; //5바이트 

	char str[] = "abcde"; //6바이트 //!!이게 중요 
	// 눈에는 안보이지만 \0 이 추가로 저장된다. 
	//char str[] = "abcde\0"; => 1바이트를 더 먹는다.  => 6바이트 
	//--------------------------------
	//int = 4바이트 
	int n;
	int arr[] = { 1,2,3,4,5 }; //4 * 5 = 20바이트 

	printf("ch ==> %ld\n", sizeof(ch)); // 1
	printf("chList ==> %ld\n", sizeof(chList)); // 5
	printf("str ==> %ld\n", sizeof(str)); // 6
	printf("n ==> %ld\n", sizeof(n)); // 4
	printf("arr ==> %ld\n", sizeof(arr) ) ; // 20 
	printf("arr개수 ==> %ld", sizeof(arr) / sizeof(int)); // 5
	return 0;
}

  • 다른 언어에는 없는. c언어에만 있는 유일한 자료형이 있는데. 그것은 배열. 순수배열 
  • 배열 
    • main에는 배열을 만들 수가 없다. 
      • 자바와 자바스크립트는 main에 배열을 못 만든다. 
      • 배열을 만들면 항상 heap 2번째 메모리에 만들어진다. 

자바. 자바스크립트

  • 유일하게 c언어는 main에다가 배열을 만들 수 있다. 

  • 수업012_배열03.cpp
#include <stdio.h>
int main(void) {

	
	char str[] = "abcde"; //6바이트
	//글자는 1바이트 

	char str2[] = "qwert"; //6바이트 

	//char str2[] = "qwert\0";
		//\0이 자동으로 들어가서 6바이트다. 
		//문자열은 인덱스가 없다.  
		//str2[i] != '\0' => 반복문을 이렇게 돌려야 한다. 
			//문자열은 

	int i = 0;
	printf("%s\n", str); //abcde 
	//s => String의 약자. 
	//알아서 반복문을 돈다. 

	//메모리에 
		//a  b  c  d e 
		//i < 5; 
	for (i = 0; str2[i] != '\0'; i++) {
		//str2[i] != '\0' 
			//str2[i] 값이 \0 이 아니면. 
		printf("%c", str2[i]); //qwert
	}
	

	return 0;
}

  • _11_문제

  • 문제012_배열.cpp
#include <stdio.h>

int main(void) {

	int n[3] = { 73, 95, 82 }; //168 + 82 =250
	int sum = 0;
	for (int i = 0; i < 3; i++) {
		sum += n[i];
	}

	//250 / 30 = 8 //
	switch (sum / 30) {
		case 10: 
		case 9: printf("A");
		case 8: printf("B");
		//여기서 부터 다 내려간다. break가 없어서. 
		case 7: 
		case 6:  printf("C");
		default:  printf("D");
	}
	return 0;
	//출력 b c d 가 나온다.
	//전부 출력이 된다. 
}

  • 문제012_배열02.cpp
#include <stdio.h>

int main(void) {

	int num[] = { 1,2,3};
	int i = 0;
	for (i = 0; i < 5; i++) { // 3, 4 번째는 쓰래기값이 출력된다.
		printf("%d ", num[i]);
	}
	return 0;
}

  • c언어는 특이한 점이 있다. 
  • int num[] = { 1,2,3};
    • 배열이 3개가 있다. 
    • 실질적으로 컴퓨터 메모리는 계속 있는거다. 
    • 운영체제가 아무거나 3개를 잡아서 여기다가 num이라고 이름을 붙인거다. 
      • 하지만 메모리는 계속 있다. 
      • c언어는 메모리를 읽을 수 있다. 
      • 명령을 안 했는데도. 벗어나서 읽을 수 있다. 
      • 만약 사용하고 있다면 에러가 난다. rock이 걸려있어서. 
      • 메모리를 안 잡아놔도. 


  • 문제012_배열03.cpp
#include <stdio.h>

int main(void) {

	char ch[5]; //5바이트 
	char str[] = "abcde"; //6바이트 
	int num[] = { 1,2,3,4,5 }; //20바이트 
	//int는 4바이트니까. 4 * 5 
	//int는 \0이 없다. 
	printf("%ld, ", sizeof(ch));//5
	printf("%ld, ", sizeof(str));//6
	printf("%ld, ", sizeof(num) / sizeof(int));//5
	return 0;
}

  • 문제012_배열04.cpp
#include <stdio.h>

int main(void) {
	
	int a[] = { 95, 75, 100, 50 };
	
	int i, j, temp;

	int n = sizeof(a) / sizeof(int);
	//16 / 4 = 4 / n = 4 


	for (i = 0; i < n - 1; i++) {
		// 0 , 1 , 2, 3 반복문 
		for (j = 0; j < 3 - i; j++) {
			//012, 01, 0 
			//6번 돌음 

			if (a[j] > a[j + 1]) {
				// 75, 95, 100,50
				//75,95,50,100
				//75,50,95,100
				//50,75,95,100
				temp = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp;
			}
		}
	}

	for (i = 0; i < 4; i++)
		printf("%d ", a[i]);

	return 0;
}

  • 문제012_배열05.cpp
#include <stdio.h>

int main(void) {

	char msg[50] = "Hello World! Good Luck!";
	int i = 2, number = 0;

	while (msg[i] != '!') {
		//He  llo World => 여기까지 돈다. 
			//i = 2이기 때문에. 
			//llo World => 여기서부터 돈다. 

		if (msg[i] == 'a' || 
			msg[i] == 'e' || 
			msg[i] == 'i' || 
			msg[i] == 'o' || 
			msg[i] == 'u')
			number++;
		i++;
	}  
	printf("%d ", number); //2

	return 0;
}

  • 문제012_배열06.cpp
#include <stdio.h>

int main(void) {

	int i;
	char ch;
	char str[7] = "nation";
	//char str[6] 이렇게 쓰면 큰일 난다. 0안들어가서. 반복문 무한으로 돌음 


	//nation
	//notian
	//noitan
	//notian
	for (i = 0; i < 4; i++) {
		// 5 - i => 5, 4, 3, 2
		// i =>     0, 1, 2, 3 

		ch = str[5 - i];
		str[5 - i] = str[i];
		str[i] = ch;
	}
	printf("%s \n", str);  //notian

	return 0;
}

  • 문제012_배열07.cpp
#include <stdio.h>

int main(void) {
	int arr[] = { 10, 30, 50, 70, 90 };
	int i, max, min;
	max = arr[0];
	min = arr[0];
	
	for (i = 0; i < 5; i++) {
		if (arr[i] > max)
			max = arr[i];
		if (arr[i] < min)
			min = arr[i];
	}

	printf("%d %d", min, max); //10 90 

	return 0;
}

  • 문제012_배열08.cpp
#include <stdio.h>
#include <stdlib.h> // srand();
#include <time.h> // time(NULL)

int main(void) {
	int hits[16] = { 0, };
	int n, i = 0;
	srand(time(NULL));

	do {
		i++;
		n = rand() % 6 + 1;
		hits[i] = n;
	} while (i < 6);

	for (i = 0; i < 6; i++) {
		printf("h[%d] = %d \n", i, hits[i]);
	}
	
	return 0;
}
반응형