Tính và in ra trung bình cộng của các số âm, số dương trong array Pascal

Tính và in ra trung bình cộng của các số âm, số dương trong array Pascal

Viết chương trình nhập số nguyên N (0 < N ≤ 10 000) và mảng a gồm N số nguyên. Tính và in ra trung bình cộng của các số âm, số dương trong a. Không tính số 0.

Ví dụ : a[] = {1, -6, 0, 3, -1}. Trung bình cộng là -0.75

Bài giải

PROGRAM tbc_am_duong_mang;

USES crt;

VAR a:ARRAY[1..100] OF REAL;
    i,n,x,y:INTEGER;
    tongam,tongduong,tba,tbd:REAL;

BEGIN
    clrscr;
    write('So phan tu cua day n= ');readln(n);
    FOR i:=1 TO n DO
        BEGIN
            write('Nhap phan tu a[',i,']: ');
            readln(a[i]);
        END;
   
    tongam:=0; x:=0;
    FOR i:=1 TO n DO
    IF a[i]<0 THEN
        BEGIN tongam:=tongam+a[i];
              x:=x+1;
        END;
    tba:=tongam/x;
    write('TB cong cac so am la: ',tba:5:1);
   
    writeln;
   
    tongduong:=0; y:=0;
    FOR i:=1 TO n DO
    IF a[i]>0 THEN
        BEGIN tongduong:=tongduong+a[i];
              y:=y+1;
        END;
    tbd:=tongduong/y;
    write('TB cong cac so duong la: ',tbd:5:1);
   
    readln;
END. 
Bạn thấy bài viết này như thế nào?: 
Average: 8.8 (5 votes)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Bình luận (0)

 

Add Comment

Filtered HTML

  • Các địa chỉ web và email sẽ tự động được chuyển sang dạng liên kết.
  • Các thẻ HTML được chấp nhận: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Tự động ngắt dòng và đoạn văn.

Plain text

  • No HTML tags allowed.
  • Các địa chỉ web và email sẽ tự động được chuyển sang dạng liên kết.
  • Tự động ngắt dòng và đoạn văn.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Cách làm Drupal to Ember without Saving năm 2016

Cách làm Drupal to Ember without Saving năm 2016

Running headless Drupal with a separate javascript framework on the front-end can provide amazing user experiences and easy theming

[Phần 6] - Testing: Unit và Functional với PHPUnit

[Phần 6] - Testing: Unit và Functional với PHPUnit

So far we have explored a good amount of ground looking at a number of core concepts with regards to Symfony2 development. Before we continue adding features it is time to introduce testing.

Viết module cho Drush command để xoá files như INSTALL.txt, UPGRADE.txt...

While learning to use Drush I was amazed at how easy it made updating the Drupal core and community modules.

Công ty diệt chuột T&C

 

Diet con trung