0
Follow
0
View

Then calculate and output the number of all positive, negative, and zeros in the array

d510423336 注册会员
2023-02-27 16:15

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > here is a simple C language program, can realize the input 10 integers, and calculate the output of the number of positive, negative and zero:

#include 

int main()
{
    int arr[10];
    int i, pos_num = 0, neg_num = 0, zero_num = 0;

    printf("请输入10个整数:\n");
    for(i = 0; i < 10; i++)
    {
        scanf("%d", &arr[i]);
        if(arr[i] > 0)
            pos_num++;
        else if(arr[i] < 0)
            neg_num++;
        else
            zero_num++;
    }

    printf("正数个数:%d\n", pos_num);
    printf("负数个数:%d\n", neg_num);
    printf("零的个数:%d\n", zero_num);

    return 0;
}
The

program defines an integer array arr of length 10, which is used to store the input 10 integers. The program then iterates through the set of numbers through the for loop, judging the positivity of each element and counting the number of positive, negative, and zeros separately. Finally, the program outputs the statistics to the screen.

Note that in actual use, some basic checksum exception handling should be done on user input to prevent user input errors. For example, you can add some validity judgments to the input, such as determining whether the input is an integer.

ssxbg123 注册会员
2023-02-27 16:15

#include 
int main()
{
    int i, m = 0, n = 0,b=0;
    int a[10];
    printf("请输入10个整数:\n");
    for (i=0; i<10; i++)
    {
        scanf("%d", &a[i]);
        if (a[i] > 0)
        {
            m++;
        }
        if (a[i] < 0)
        {
            n++;
        }
        if (a[i] == 0)
        {
            b++;
        }
    }
    printf("正数个数:%d\n", m);
    printf("零的个数:%d\n", b);
    printf("负数个数:%d", n);
}
.
< !- - - - - >
dengmian1314 注册会员
2023-02-27 16:15
); for (i= 0 ;i<= 9 ;i++) { scanf ( "%d" ,&a[i]); if (a[i]> 0 ) m++; else if (a[i]== 0 ) n++; } printf ( "整数:%d,零:%d个,负数:%d个" ,m,n,( 10 -m-n)); return 0 ; } .
< !- - - - - >
inu1120 注册会员
2023-02-27 16:15
< div class = "md_content_show e397 data - v - 3967" = "" >

hope to adopt

yantafeizei 注册会员
2023-02-27 16:15

This answer quotes ChatGPT

Here is a program written in C# to input 10 integers, store them in an array, and then calculate and output the number of all positive, negative, and zeros in the array.

using System;

class Program
{
    static void Main(string[] args)
    {
        int[] numbers = new int[10];
        int positiveCount = 0, negativeCount = 0, zeroCount = 0;

        Console.WriteLine("请输入10个整数:");

        // 读取输入的10个整数并存储在数组中
        for (int i = 0; i < 10; i++)
        {
            Console.Write("第{0}个数:", i + 1);
            numbers[i] = Convert.ToInt32(Console.ReadLine());
        }

        // 计算数组中的正数、负数和零的个数
        foreach (int number in numbers)
        {
            if (number > 0)
            {
                positiveCount++;
            }
            else if (number < 0)
            {
                negativeCount++;
            }
            else
            {
                zeroCount++;
            }
        }

        // 输出结果
        Console.WriteLine("数组中有{0}个正数,{1}个负数,{2}个零。", positiveCount, negativeCount, zeroCount);
    }
}

In this program, we first declare an array of 10 integers, numbers, and the variables positiveCount, negativeCount, and zeroCount, which store the number of positive, negative, and zeros, respectively.

Then, we use a for loop to read 10 integers and store them in the array numbers. Next, you use a foreach loop to iterate through each element in the group, incrementing the counters for positive, negative, and zero based on the element's value. Finally, we output the counter value to get the number of all positive, negative, and zeros in the array.

tmm6213856 注册会员
2023-02-27 16:15
< div class = "md_content_show e397 data - v - 3967" = "" >

this is not a c #, topic main you not want to c

About the Author

Question Info

Publish Time
2023-02-27 16:15
Update Time
2023-02-27 16:15

Related Question