민트맛뚱이
kwangkwang우럭따
민트맛뚱이
전체 방문자
오늘
어제
  • 분류 전체보기 (16)
    • C (12)
    • C programming (2)
    • digital engineering (1)
    • machine learning (0)
    • Data communication (0)
    • signal processing (0)
    • computer architecture (RISC.. (0)
    • project (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
민트맛뚱이

kwangkwang우럭따

뚱이가 알려주는 소프트웨어

(C언어) 모의중간고사, 중간고사
C

(C언어) 모의중간고사, 중간고사

2022. 12. 17. 21:27

네,,,C언어 중간고사를 봤습니다. 그때 대학교 첫 시험이라 매우 떨렸지만 부술 수 있습니다!

문제가 어떤건지 말해줘도 되려나,,, 뭐.. 연도마다 바뀌니깐..ㅎㅎ 그냥 올릴게요

 

일단 모의 중간고사부터!!


C programming, mid-term Exercise

April 10, 2019

 

1.      Write a program to input five integers between 1 to 4 (You must use each integer at least once.) and print them on the screen as follows:

         Example:

            Input: 1 3 4 3 2

            Output:

            Sum of the values 13

            Average of the values 2.6

            The mode of the values 3

   ------------------------------------------------------------------------- 4 point

2.      Input your name as lower and store it in char array and print upper characters as follows:

             Example:

            Input: hong gil dong

            Output: HONG GIL DONG

------------------------------------------------------------------------- 5 point

3.      Write a program to input one integer and print sum of the primes smaller then integer you entered. Hint: use nesting loop

       Example:

Input: 10

             Output: 17         

------------------------------------------------------------------------- 6 point

4.      Write a program that prints following pattern. You must use for loop.

----------------------------------------------------------------------- 7 point

5.      Input your name and store it in char array. Also, input one letter to count. You have to print number of character in the name and position of the letter.     

Example:

             Input Name: hong gil dong

             Input letter: n

             Output:

             The position of letter:  3  12

             The number of letter in the name:  2

------------------------------------------------------------------------- 8 point

TOTAL: 30 points                       Made by. Ditto group 10 mentors. 2019 / 04 / 01.

 

 

 

#1. 

#include <stdio.h>

int main(void)
{
	int number[5];		// 수 5개를 입력받을 배열 선언 
	int sum=0, mode, mode_count = 0, count=0;	// 합계, 최빈값, 최빈값이 나온 수, 나온 숫자 개수를 셀 변수 선언 
	float avg;		// 평균을 저장할 실수형 변수 선언 
	
	printf("Input : ");		// 사용자에게 값을 입력받음 
	
	for(int i=0; i<5; i++)		// 총 5번 반복하여 
	scanf("%d",&number[i]);		// 사용자에게 입력받은 값을 각 배열에 저장 
	
	for(int i=0; i<5; i++)		// 여기서 각 배열에 저장된 값을 계속 더해 합계를 계산 
	sum = sum + number[i];	 	// sum += number[i]; 라고 써도 무방! 
	
	avg = (float)sum/5;		// 평균값을 합계를 5로 나눈 값으로 두되, sum이 int형임을 고려해 float으로 형변환 
	
	for(int i=0; i<5; i++)	// 총 5번 반복 
	{
		count = 0;		// 센 숫자 갯수를 우선 0으로 초기화해줌  
		for(int j=0; j<5; j++)		// 이제 5번을 반복하되, 
		if(number[i] == number[j]) count++;		// 만약 number[i]가 j번째 수와 같을 경우 count++로 하나 더해줌 
		
		if(mode_count < count)	// 만약 이전에 최빈값으로 정해졌던 값보다 더 많이 있다면 
		{
			mode = number[i];	// 새로이 number[i]를 최빈값으로 처리하고 
			mode_count = count;	// 그때의 count값을 새로이 mode_count에 저장 
		}
	}
	
	printf("\nSum of the value : %d",sum);		// 합계 출력 
	printf("\nAverage of the value : %g",avg);	// 평균 출력 
	printf("\nThe mode of the values : %d",mode);	// 최빈값 출력 
	
	return 0;		// 모든 명령을 끝냈으므로 0을 return 하고 종료 
}

 

#2.

#include <stdio.h>

int main(void)
{
	char name[20];	// 이름을 입력받을 배열 name 선언 
	
	printf("Input : ");		// 사용자에게 이름을 입력할 것을 요구 
	scanf("%[^\n]s",name);		// 사용자에게 입력받은 값은 name에 저장 
	
	for(int i=0; name[i]; i++)	// name[0]부터 name배열의 유의미한 값이 있는 부분까지 반복 
	{
		if(name[i] == ' ')		// 만약 name[i]가 띄어쓰기라면 값을 변경해선 안되므로 
			printf(" ");	// 그대로 띄어쓰기 " " 로 출력 
		else printf("%c",name[i] + 'A' - 'a'); 	// 그게 아닐경우 A와 a 사이 아스키 값의 차이를 이용해 대문자로 변환 
	} 
	
	return 0;	// 프로그램의 목적을 달성했으므로 return 문으로 0 반환 후 종료 
}

 

#3. 

#include <stdio.h>

int main(void)
{
	char name[20];	// 이름을 입력받을 배열 name 선언 
	
	printf("Input : ");		// 사용자에게 이름을 입력할 것을 요구 
	scanf("%[^\n]s",name);		// 사용자에게 입력받은 값은 name에 저장 
	
	for(int i=0; name[i]; i++)	// name[0]부터 name배열의 유의미한 값이 있는 부분까지 반복 
	{
		if(name[i] == ' ')		// 만약 name[i]가 띄어쓰기라면 값을 변경해선 안되므로 
			printf(" ");	// 그대로 띄어쓰기 " " 로 출력 
		else printf("%c",name[i] + 'A' - 'a'); 	// 그게 아닐경우 A와 a 사이 아스키 값의 차이를 이용해 대문자로 변환 
	} 
	
	return 0;	// 프로그램의 목적을 달성했으므로 return 문으로 0 반환 후 종료 
}

 

#4. 

#include <stdio.h>

int main(void)
{
	for(int i=0; i<5; i++)	// 세로로 5줄이므로 5회 반복 
	{
		printf("*");  // 우선 가장 왼쪽의 *는 위치가 고정이므로 그대로 출력 
		for(int j=0; j<i; j++) 	 // 중간의 *을 출력하는 위치를 결정하는 변수 j 선언 후 0으로 초기화함. 
		printf(" ");   // 이제 i열이면, i만큼 띄어쓰기를 하여 j 위치 선정  
		printf("*");   // 이제 *의 위치가 정해졌으므로 *를 출력함 
		for(int j=5-i; j>0; j--) printf(" ");	// 이제 남은 칸수만큼 다시 띄어쓰기 출력 후   
		printf("*\n");	// *를 출력해 오른쪽을 완성 후 줄바꿈 
	}
	return 0;	// 프로그램의 목표를 달성하였으므로 return 문으로 0 을 반환 후 프로그램 종료 
}

 

#5.

#include <stdio.h>

int main(void)
{
	char name[20];	// 이름을 저장할 name 배열 선언. 
	char letter;	// 위치파악 및 셀 문자를 저장할 변수 letter 선언.  
	int count = 0;	// 문자 갯수를 셀 count 변수 선언 후 0으로 초기화 
	
	printf("Input Name : ");	// 사용자에게 이름을 입력할 것을 요구 
	scanf("%[^\n]s",name);		// name 배열에 이름 저장 
	printf("Input letter : ");	// 사용자에게 문자를 입력할 것을 요구 
	scanf(" %c",&letter);		// letter 변수에 문자 저장 
	
	printf("\nThe position of letter : ");	// 문자 위치에 대한 안내문 먼저 출력 
	
	for(int i=0; name[i]; i++)	// name[0] 부터 name배열의 유의미한 값이 끝날때까지 반복 
	{
		if(name[i] == letter)	// 만약 name[i]에 들어있는 문자가 사용자에게 입력받은 문자(=letter 변수의 값)과 같으면 
		{
			printf("%d ",i+1);	// 그 문자의 위치를 출력. 
			count++;	// 세려는 문자가 나왔으므로 count++로 1을 더해줌 
		}
	}
	
	printf("\nThe number of letter in the name : %d",count);	// 이제 문자 위치는 다 출력했으므로, 센 문자 갯수 출력 

	return 0;	// 프로그램의 목표를 달성했으므로 return문으로 0을 반환 후 프로그램 종료 
}

이런식으로 답지가 되겠죠?? 도움이 되셨으면 좋겠네요. 자 그러면 대망의 중간고사!!!!

 


사실 중간고사는 시험지가 없었기 때문에..1학년이라 사실 시험지를 다운받을 생각도 못 했음..헤헤

그래도 답은 있으니 꼭 참고하세요

 

#1. 

#include<stdio.h>
int main(){
    int a[2], i, k, l, M, m;
    printf("enter the number");
    for(i=0; i<3; i++){
	scanf("%d", &a[i]); 
	printf("%d ", a[i]);
}
	for(i=0; i<3; i++){
	printf("\n%d", a[i]); 	
	k*=a[i];
	l+=a[i];
}
    printf("\n");
    
	printf("multiplication : %d\n", l);
	printf("addition : %d\n", k);
    for(i=0; i<3; i++)
    if(a[i]%2==1)
    printf("odd : %d\n", a[i]);
    else 
    printf("even : %d\n", a[i]);
    
    m=a[0];
    M=a[0];

	for(i=0; i<3; i++){
	if(m>a[i])
	m=a[i];
	if(M<a[i])
	M=a[i];
}
	printf("the largest : %d\n", M);
	printf("the smallest : %d", m);

}

1번 과제는 보니깐 숫자를 입력받고, 홀수 짝수 구별 그리고 최소 최대 값을 출력하는 코드내용

 

 

#2.

#include<stdio.h>
int main(){
	int i, j, k, m;
	int a[3][3];
	 printf("enter the number : ");
	 
	 for(i=0; i<3; i++){
	 	for(j=0; j<3; j++){
	 scanf("%d", &a[i][j]); 
}
}
    for(i=0; i<3; i++){
	 	for(j=0; j<3; j++){
	 printf(" %d", a[i][j]);
}
	printf("\n");
}
	printf("Sum first-column : ");
    for(j=0; j<1; j++){
		for(i=0; i<3; i++){
			k += a[i][j];
	}
}
        printf("%d ", k);
        k=0;
         printf("Sum last-row : ");
	for(i=2; i<3; i++){
		for(j=0; j<3; j++){
			k += a[i][j];
			}	
}
        printf("%d", k);
        printf("\n");
        
        m=a[0][0];
         for(i=0; i<3; i++){
	 		for(j=0; j<3; j++){
	        if(m>a[i][j])
	        m=a[i][j];
}
}
         printf("the smallest number is : %d", m);
}

2번과제는 보니깐 2차원 배열을 통해서 숫자들을 받고, 첫번째 열에서 합과 마지막 행에 합, 그리고 가장 작은 숫자를 출력하는 코드네요!

 

#3. 

#include<stdio.h>
#include<string.h>
#include<ctype.h> 
main()
{
   int i=0, j, b, k;
   char a[20], n;
   printf("enter the name:");

   for(i=0; i<20; i++) 
   {
    scanf("%c", &a[i]); 
        if (a[i]== '\n')
        break;
   }
   for(j=0; j<i; j++){
   	if(a[j]==' '){
   		continue;
	   }
	   printf("%c", a[j]);	   
   }
   
   for(i=0; i<20; i++){
   	if(i==0){
   	printf("\nfirst name is %c", a[0]);
   }
   	else if(a[i+1]=='\n'){
   	printf("\nlast name is %c", a[i]);
   }
   }
   printf("\n");
   	
	for(i=0; a[i]; i++){
		printf("%c", a[i]);
		if(a[i]==' ')
		break;
   } 
   	for(i=0; a[i]; i++){
	if(a[i]==' ')
    a[i]=toupper(a[i+1]);
    if(isupper(a[i]))
	printf("%c", a[i]);
}
   }

마지막 코드는 이름을 입력받고, 처음 이름과 마지막 이름을 받은 후, 공백 뒤 문자가 소문자일 경우 대문자로 바꿔주는 코드네요~~!!

 

네 이상 중간고사 끝이었습니다~~~~!!!!~~!~!


 

이 그림을 보고 뭐지,,중간고사 관련 공지가 없나 싶었는데,

ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ 이상한 사람,,,

'C' 카테고리의 다른 글

(C언어) 기말고사 전 과제들 - (1)  (1) 2022.12.17
(C언어) 기말고사 전 과제들  (1) 2022.12.17
(C언어) 중간고사 전 과제들 - (3)  (2) 2022.12.17
(C언어) 중간고사 전 과제들 - (2)  (0) 2022.12.17
(C언어) 중간고사 전 과제들 - (1)  (0) 2022.12.17
    민트맛뚱이
    민트맛뚱이
    암 벅

    티스토리툴바