仅显示负数的程序

A program that will show only negative numbers

本文关键字:程序 显示      更新时间:2023-10-16

我需要编写一个程序,该程序将仅显示给定向量的负数。我做到了,但它不起作用。有人可以告诉我为什么这段代码不起作用吗?

int main(int argc, char *argv[])
{   
     int const m = 6;
     int A[m] = { 1, -2, 3, -4, 5, -6 };
     int i;
     std::cout << "A[negative]={";
     for (i = 0; i < m; i++)
         if (A[i] < 0)
             std::cout << A[i];
     std::cout << "}" << std::endl;`
     system("PAUSE");
     return EXIT_SUCCESS;
}

程序运行良好 我只添加了#include <iostream>并将 return 语句更改为以下return 0;

我不知道你用的是什么IDE,但如果你使用的是Visual Studio。最好执行以下操作,而不是使用system("PAUSE");

转到"项目>属性">链接器>系统>子系统>,然后从下拉菜单中选择">控制台"(/子系统:控制台)

#include <iostream>
int main(int argc, char *argv[])
{
    int const m = 6;
    int A[m] = { 1, -2, 3, -4, 5, -6 };
    int i;
    std::cout << "A[negative]={";
    for (i = 0; i < m; i++)
        if (A[i] < 0)
            std::cout << A[i];
    std::cout << "}" << std::endl;
    return 0;
}