Super Kawaii Cute Cat Kaoani
본문 바로가기
💻 Programming/C | C++

[C++] 코딩 과제 7

by wonee1 2023. 1. 6.
728x90

7.1 (등급결정-배열)

학생 점수를 입력하고,가장 좋은 점수를 구한 후, 다음 규칙에 따라 등급을 결정하는 프로그램을 작성하여라
점수>=최고점-10 이라면 A등급
점수>=최고점-20 이라면 B등급
점수>=최고점-30 이라면 C등급
점수>=최고점-40 이라면 D등급
나머지는 F등급
 
학생의 수를 입력하고, 각 학생들의 점수를 입력한 다음, 등급 결과를 출력하는 프로그램을 작성하여라
 
 

#include <iostream>
using namespace std;
void print_score(int data[], int size);
void read_data(int data[], int size);
void read_data(int data[], int size);

void print_data(int data[], int size) {

    for (int i = 0; i < size; i++)cout << data[i] << " ";


}
void read_data(int data[], int size) {

    for (int i = 0; i < size; i++)cin >> data[i];


}
void print_score(int data[], int size){

    int max, i;

    max = data[0];
    for (int i = 0; i < size; i++) {

        if (max < data[i])max = data[i];
    }

    for (int i = 0; i < size; i++) {

        if (data[i] >= (max - 10)) {
            cout << "Student" << " " << i << "score is" << " " << data[i] << " " << "and grade is ";
            cout << "A";
            cout << endl;
            
        }

        else if (data[i] >= (max - 20)) {
            cout << "Student" << " " << i << "score is" << " " << data[i] << " " << "and grade is ";
            cout << "B";
            cout << endl;
            
        }
        else if (data[i] >= (max - 30)) {
            cout << "Student" << " " << i << "score is" << " " << data[i] << " " << "and grade is ";
            cout << "C";
            cout << endl;
        }
        else if (data[i] >= (max - 40)) { 
            cout << "Student" << " " << i << "score is" << " " << data[i] << " " << "and grade is ";
            cout << "D";
            cout << endl;
        }
        else {
         
            cout << "F";
           
            
        }

    }

}



int main() {
    const int SIZE = 100;
    int n;
    int data[SIZE];

    cout << "Enter the number of students:"; cin >> n;
    cout << "Enter"<<" " << n << " scores:";


    read_data(data,n);
    print_score(data, n);
 

    return 0;

}

 

실행결과

 
 


 
 

7.4 (점수분석-배열)

지정되지 않은 개수의 점수를 입력하고 평균과 같거나 큰 점수의 수와 평균보다 작은 수의 개수를 계산하는 프로그램을 작성하여라. 입력이 끝이라는 것을 지시하기 위해서는 음수를 입력한다. 입력점수의 최대개수는 100개이다.

#include <iostream>
using namespace std;

int main() {

	const int SIZE = 100;
	double score[SIZE] = { 0, };
	double sum = 0;
	int n = 0;

	cout << "배열 입력:";

	do {
		cin >> score[n];

		if (score[n] != -1) {
			sum += score[n];
		}
	} while (score[n++] >= 0);

	double average = sum / (n - 1); //평균

	int emax = 0;
	int under = 0;

	for (int i = 0; i < n - 1; i++) {
		if (score[i] >= average)
			emax++;
		else
			under++;

	}

	cout << "평균:" << average << endl;
	cout << "평균보다 큰 값" << emax << endl;
	cout << "평균보다 작은 값" << under << endl;

	return 0;



}
실행결과

 


 
 
7.5(점수 중복제거 -배열)

10개의 숫자를 읽어 중복된 숫자는 한 번만 출력되도록 하는 프로그램을 작성하여라 (힌트: 숫자를 읽어 그 수가 새로 들어온 수이면 배열에 저장하고,이미 있는 배열에 존재하고 있는 숫자라면 입력 값을 버린다. 이렇게 하면 배열에는 중복된 숫자가 없게 된다

#include <iostream>
using namespace std;
int main()
{
	const int SIZE = 10;
	int a[SIZE];
	int b[SIZE];

	cout << "Enter ten numbers:";

	for (int i = 0; i < SIZE; i++) {
		cin >> a[i]; // 배열 입력
	}
	

	int j, k, q = 0;

	for (j = 0; j < 10; j++)
	{
		for (k = 0; k < j; k++)
		{
			if (a[j] == a[k])//중복제거
				break;
			
		}
		if (j == k) {
			b[q] = a[j]; //a를 b에 담는다 
			q++;
		}
	
	}

	cout << "The distinct numbers are:";

	for (j = 0; j < q; j++)
	{
		cout << b[j] << " "; //중복 제거한 배열을 출력 
		
	}

	return 0;
}

 
 

실행 결과

 


 
 
7.7(발생빈도-배열,랜덤넘버 생성)

0과 9사이의 임의의 정수 100개를 생성하여 각 숫자의 개수를 출력하는 프로그램을 작성하여라(힌트:0에서 9사이의 임의의 정수는 rand()%10으로만들 수 있다. counts라는 이름으로 정수 10개를 저장할 수 있는 배열을 작성하고, 0,1,...,9의 개수를 해당 배열 요소로 저장한다.
 

#include <iostream> 
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	int counts[10]={0,};
	
	srand(time(0));

	for (int i = 0; i < 100; i++)
		counts[rand() % 10]++;

	for (int i = 0; i < 10; i++)
		cout << i<<" " << "갯수:" << counts[i] << endl;

	return 0;
}

 
 

 
 


7.22(조합 생성-배열)

10개의 정수를 입력 후 입력 수로 부터 두 수를 뽑는 조합 (중복미허용)
 

#include <iostream>
using namespace std;
void comb(int data[], int choice[], int num, int k);
void comb(int data[],int choice[],int num, int k) {


	if(k> 2){
		for (int i = 1; i <= 2; i++) {
			cout << choice[i]<<" ";
			
		}
		cout << endl;
		return;
	}


	if (num > 10)return;

	
	choice[k] = data[num];
	
	

	comb(data, choice,num+ 1, k+ 1);
	comb(data, choice, num + 1, k);

}

int main() {

	int data[11];
	int choice[10];

	for (int i = 1; i <= 10; i++)cin >> data[i];

	comb(data,choice, 1, 1);


	return 0;
}

 

728x90

'💻 Programming > C | C++' 카테고리의 다른 글

[C++] 코딩과제 10  (0) 2023.01.06
[C++] 코딩과제 9  (0) 2023.01.06
[C++] 코딩과제 8  (0) 2023.01.06
[C++] 코딩과제 1~6  (0) 2022.11.01