0

How would I go about making a C program that takes in input from the user (of an integer like -232, or 14) and print the integer with the greatest value the user inputted?

So far all I know is (my pseudocode):

int main(void)
{

    int variable;

    printf("Enter an integer to check if that is the greatest integer you inputted.")

    if %d > variable;
        printf("The greatest value you entered is %d")
    elif
        printf("The greatest value you entered is 'variable'")

    scanf("%d", &variable) /Will this command help? IDK
}

I don't want the actual code, but the steps/commands I would need to do so. Sorry for making it seem as though I am making others do my work for me. I just started C and I am not very familiar with it :(

Thanks.

PS The program is supposed to store and keep record of the greatest integer inputted.

lurker
  • 56,987
  • 9
  • 69
  • 103
user3326078
  • 617
  • 1
  • 9
  • 19
  • possible duplicate of [MIN and MAX in C](http://stackoverflow.com/questions/3437404/min-and-max-in-c) – simonzack Sep 06 '14 at 00:32
  • 2
    What you have isn't really pseudo-code but very broken C code. If you aren't familiar with C, I would suggest going through an online tutorial or get an introductory C book. As @McLovin suggests, you're going to need a loop (in C, a `while` would do work here). And you'll need to start with `variable` initialized to it's lowest possible value, or have a separate flag indicating whether you've read the first value yet. In the loop, replace `variable` with the next read integer whenever it is greater than `variable`. The loop ends when there's no more input. Then you print `variable`. – lurker Sep 06 '14 at 00:32
  • Does the user enter one integer? Then what should it be compared with? – Spikatrix Sep 06 '14 at 02:52

2 Answers2

0

You need two things, One is what you are looking for, and One is your end case (when will you stop looking)

You are looking for the largest number, but when do you stop looking? After 10 values? After the end of the file? After a new line?

So in Pseudo code its like

int i = 0;
int variable = 0; //Good practice to initialize your variables.
while(When will you stop? i < 10 eg 10 inputs?){
    if(your input is > variable){
        variable = input;
    }
i++; //or whatever your end case is. Have to get closer to the end case.
return variable;
Chocobuny
  • 11
  • 1
0
#include <limits.h>
#include <stdio.h>

int main(void)
{
    int greatest = INT_MIN, variable;
    FILE *fp;
    (fp = fopen("record.txt", "a")) &&
    (fp = freopen("record.txt", "r+", fp));
    if (!fp) return perror("record.txt"), 1;

    fscanf(fp, "%d", &greatest);
    printf("Enter an integer to check if that is"
           " the greatest integer you inputted. ");
    if (scanf("%d", &variable) == 1)
      if (variable > greatest)
          rewind(fp), fprintf(fp, "%d\n", greatest = variable);
    printf("The greatest value you entered is %d\n", greatest);
}
Armali
  • 18,255
  • 14
  • 57
  • 171