标题:谁能帮我看看错在哪了?
只看楼主
amsteven
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-5-21
结帖率:0
已结贴  问题点数:20 回复次数:1 
谁能帮我看看错在哪了?
Write a C program that reads in 15 integer numbers from the keyboard to an
array (2p). Print out the largest negative number of the array (2p). Write and
use your own function to decide whether a number is negative or not (2p).
Example run:
 Give a number please... -2
 Give a number please... 5
 Give a number please... -7
 Give a number please... 3
...
 Give a number please... 4
 The number with the largest abs value: -2
****************************************************
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int isnegative(int ar[5]){
    int i;
    for(i=0;i<5;i++)
    if(ar[i]<0);   
}

int main(int argc, char *argv[]) {
    int ar[5],i,min=0;
    for(i=0;i<5;i++){
        printf("give me a number ");
        scanf("%d",&ar[i]);
    }
   
    for(i=0;i<5;i++){
    if(ar[i]<0){
   
    if(isnegative(ar[i])>min) min=ar[i];
}
}
    printf("%d",min);
    return 0;
}
搜索更多相关主题的帖子: function keyboard include numbers whether 
2015-05-21 16:29
calix
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:28
帖 子:249
专家分:1442
注 册:2015-5-4
得分:20 
main方法中的第二个循环应该不需要,直接通过isnegative方法返回最大负数
程序代码:
int isnegative(int ar[5]){
    int i = 0, min = 0;
    for(i=0;i<5;i++){
        if(ar[i]<0){
            if((min < 0 && min < ar[i]) || min == 0){
                min = ar[i];
            }
        }
    }
    return min;
}

2015-05-21 18:17



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-445579-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.481370 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved