Практикум по программированию. Основы. Массивы. Процент полученных оценок

From AsIsWiki
Jump to: navigation, search

Назад · Задачи · Дальше


Contents

Java

public class Task20 {

    private static final int n = 25;

    public static void main(String[] args) {
        
	System.out.println();
        System.out.println(" Статистическая обработка результатов контрольной работы ");
        System.out.println("---------------------------------------------------------");
	
	int a[] = new int[n];

	System.out.print(" Оценки:");

	for (int i = 0; i < n; i++) {
	    a[i] = (int) (2 + 4 * Math.random());
	    System.out.print(" " + a[i]);
	}

	int s = 0, pt = 0, ch = 0, tr = 0, dv = 0;

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

	    s += a[i];

	    if (a[i] == 2) {
		dv++;
	    }
	    if (a[i] == 3) {
		tr++;
	    }
	    if (a[i] == 4) {
		ch++;
	    }
	    if (a[i] == 5) {
		pt++;
	    }
	}

        System.out.println("\n---------------------------------------------------------");
	System.out.printf(" Пятерок:  %5.2f%%\n", (double) pt / s * 100);
	System.out.printf(" Четверок: %5.2f%%\n", (double) ch / s * 100);
	System.out.printf(" Троек:    %5.2f%%\n", (double) tr / s * 100);
	System.out.printf(" Двоек:    %5.2f%%\n", (double) dv / s * 100);
    }
}


C++

// g++ 4.2

#include <iostream>

using namespace std;

int main() {

    cout << "\n Статистическая обработка результатов контрольной работы\n";
    cout << "---------------------------------------------------------\n";
    
    srand((int)time(0));
    
    const int n = 25;
    
    int a[n];
    
    cout << " Оценки:";
    
    for (int i = 0; i < n; i++) {
        a[i] = 2 + (rand() % 4);
        cout << " " << a[i];
    }
    
    int s = 0, pt = 0, ch = 0, tr = 0, dv = 0;
    
    for (int i = 0; i < n; i++) {
        
        s += a[i];
        
        if (a[i] == 2) {
            dv++;
        }
        if (a[i] == 3) {
            tr++;
        }
        if (a[i] == 4) {
            ch++;
        }
        if (a[i] == 5) {
            pt++;
        }
    }
    
    cout << "\n---------------------------------------------------------\n";
    printf(" Пятерок:  %5.2f%%\n", (double) pt / s * 100);
    printf(" Четверок: %5.2f%%\n", (double) ch / s * 100);
    printf(" Троек:    %5.2f%%\n", (double) tr / s * 100);
    printf(" Двоек:    %5.2f%%\n\n", (double) dv / s * 100);
    
    return 0;
}


Pascal




Назад · Задачи · Дальше

Personal tools
Namespaces

Variants
Actions
Navigation
Tools