-4

Here is what i'm trying to do: My teacher gave me an assignment to recieve input from a user like "1,2,3,4,-3,17,-9,0,5,-8,10" and after the user has pressed enter key the program should ignore the "," signs and print the maximum and minimum values of the positive and negative numbers (each). now he said we should use getchar() to do this but I don't understand how... I know that this function is used to read a single character from the user (and than print it with putchar() ) and not for ignoring characters, so I would like to understand how I should use it to do this kind of operation in a while loop.

So to summarize my question is about selectively reading the input (the other things I will manage myself although if someone sees something in the code and can give a tip I would be happy but im looking to understand the getchar() function to do the opposite of what i'm reading it designed to do), I have wrote and deleted the code thousands of times and couldn't get it to work... this is what it looks like now (and it doesn't work):

#include <stdio.h>
#include <stdlib.h>

int main(){

char enter = "\n";
int list_of_nums[100];
char ch1[100];
int neg_num[100];
int pos_num[100];

printf("please enter numbers separated by ',': \n");


while (getchar()!= enter) {
    scanf("%d" ,&list_of_nums;
    if (list_of_nums >= 0){
        getchar();
       pos_num.append(list_of_nums);
    }
    else{
                neg_num.append(list_of_nums);

        }
}



 int max_pos = max.pos_num[];
 int min_pos = min.pos_num[];
 int max_neg = max.neg_num[];
 int min_pos = min.neg_num[];

 printf("the maximum number of all the positives is: %d \n" ,max_pos);
 printf("the minimum number of all the positives is: %d \n" ,min_pos);
 printf("the maximum number of all the negatives is: %d \n" ,max_neg);
 printf("the minimum number of all the negatives is: %d \n" ,min_neg);


 return 0;



}

1 Answers1

0

Based on the first input choose what happens in the next one. Here's the code for the negative numbers. Just add the case with double-digit numbers and optimize it:

#include <stdio.h>
#include <stdlib.h>
#define MAX_NUMBERS 100

int main(void){
    int list_of_nums[MAX_NUMBERS];
    int pos_nums[MAX_NUMBERS];
    int neg_nums[MAX_NUMBERS];
    int size = 0, size_pos = 0, size_neg = 0, i, max_pos, min_pos, max_neg, min_neg, neg_flag = 0;
    char input;

    printf("Enter numbers separated by ',':\n");

    do {
        input = getchar();
        if(input == '-') {
            neg_flag = 1;   //for negative numbers
            input = getchar();
        }
        if(input >= '0' && input <= '9') {
            if(neg_flag) {
                list_of_nums[size] = -(input - 48); //FROM ASCII CHARACTER CODE
            } else {
                list_of_nums[size] = input - 48;
            }
            neg_flag = 0;
            size++;
        } else {
            neg_flag = 0;
        }
    } while(input != '\n');

    for(i = 0; i < size; i++) {
        printf("%d ", list_of_nums[i]);
        if(list_of_nums[i] >= 0) {
            pos_nums[size_pos] = list_of_nums[i];
            size_pos++;
        }
        else {
            neg_nums[size_neg] = list_of_nums[i];
            size_neg++;
        };
    }
    printf("\n");

    max_pos = pos_nums[0];
    min_pos = pos_nums[0];
    for(i = 1; i < size_pos; i++) {
        if(max_pos < pos_nums[i]) max_pos = pos_nums[i];
        if(min_pos > pos_nums[i]) min_pos = pos_nums[i];
    }

    max_neg = neg_nums[0];
    min_neg = neg_nums[0];
    for(i = 1; i < size_neg; i++) {
        if(max_neg < neg_nums[i]) max_neg = neg_nums[i];
        if(min_neg > neg_nums[i]) min_neg = neg_nums[i];
    }

    printf("The maximum number of all the positives is: %d\n", max_pos);
    printf("The minimum number of all the positives is: %d\n", min_pos);
    printf("The maximum number of all the negatives is: %d\n", max_neg);
    printf("The minimum number of all the negatives is: %d\n", min_neg);

    return 0;
}

P.S. For future, don't post "need help with homework" questions but try to go as far as possible in solving the task. That way you'll be able to present more researched question.

Physx
  • 128
  • 9