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

From AsIsWiki
(Difference between revisions)
Jump to: navigation, search
 
Line 9: Line 9:
  
 
<pre>
 
<pre>
import java.util.Scanner;
 
import java.lang.Math;
 
 
 
public class Task20 {
 
public class Task20 {
  
Line 17: Line 14:
  
 
     public static void main(String[] args) {
 
     public static void main(String[] args) {
       
 
        Scanner in = new Scanner(System.in);
 
 
          
 
          
 
System.out.println();
 
System.out.println();
Line 66: Line 61:
  
 
<pre>
 
<pre>
 +
// 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;
 +
}
 
</pre>
 
</pre>
  

Latest revision as of 08:01, 21 April 2015

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


Contents

[edit] 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);
    }
}


[edit] 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;
}


[edit] Pascal




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

Personal tools
Namespaces

Variants
Actions
Navigation
Tools