Novice in C needs help


 
Thread Tools Search this Thread
Top Forums Programming Novice in C needs help
# 1  
Old 01-09-2011
Novice in C needs help

Guys can you help me ?

I'm novice in C but I have a lot of will to master C. Each gurney starts with first step

O.K. My problem is to compare two integers entered by user

I know how to set up conditions and problem is how to print correct value (grater number)

Code:
// This program compares two integers and prints which number is 
// greatest number from given input

#include <stdio.h> 

int main(void) 
{
    int a, b;     // two integers 
    
    // Program shell ask user for input and store input in comp. memory
    
    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);
    
    // Tests which is greater using if statements 
    
    if ( a > b && b < a || b > a && a < b) 
         printf("Greatest: %d\n", a);
    else
         printf("Smallest: %d\n", b);
         
         
    getchar();
    getchar();
         
    return 0;
}

I know problem is red marked area. How to correct that ?
# 2  
Old 01-09-2011
Here are two ways of doing it
Code:
    // Tests which is greater using if statements
    if ( a > b)
        printf("Greatest: %d\n", a);
    else
        printf("Greatest: %d\n", b);

    // IIF ternary operator
    printf("Greatest: %d\n", a>b?a:b);

This User Gave Thanks to fpmurphy For This Post:
# 3  
Old 01-10-2011
Hi Solaris User,

The problem is not with your understanding of "how to print" something. I think you are a bit confused about how to set up "conditions." The easiest way is as follows:

Code:
if ( a > b)
        printf("Greatest: %d\n", a);
    else
        printf("Greatest: %d\n", b);

This User Gave Thanks to jaywalker For This Post:
# 4  
Old 01-10-2011
Quote:
Originally Posted by solaris_user
...
Code:
...
    if ( a > b && b < a || b > a && a < b) 
         printf("Greatest: %d\n", a);
  else
       printf("Smallest: %d\n", b);
...

...
Note that the "if" condition will always be true if "a" and "b" are distinct integers. If a > b, then the red part is true:

Code:
   if ( a > b && b < a || b > a && a < b) 

And if a < b, then the other red part is true:

Code:
   if ( a > b && b < a || b > a && a < b) 

So the control will go to the "if" branch as long as "a" and "b" are distinct (because of the "||" operator), and you'll see "Greatest: <n>".

If "a" and "b" are the same, then the control will go to "else" part, so you'll see "Smallest: <n>".

Code:
$
$ cat greater.c
// This program compares two integers and prints which number is
// greatest number from given input
#include <stdio.h>
int main(void)
{
   int a, b;     // two integers
   // Program shell ask user for input and store input in comp. memory
   printf("Enter two integers: ");
   scanf("%d %d", &a, &b);
   // Tests which is greater using if statements
   if ( a > b && b < a || b > a && a < b)
        printf("Greatest: %d\n", a);
   else
        printf("Smallest: %d\n", b);
   getchar();
   getchar();
   return 0;
}
$
$ # Control goes to "if" branch for the next two cases
$ ./greater
Enter two integers: 10 99
Greatest: 10
 
$
$ ./greater
Enter two integers: 99 10
Greatest: 99
 
$
$ # Control goes to "else" branch for the next case
$ ./greater
Enter two integers: 99 99
Smallest: 99
 
$
$

tyler_durden
These 2 Users Gave Thanks to durden_tyler For This Post:
# 5  
Old 01-13-2011
Thanks guys on your help. I understand better logical expressions. I'm trying to resolve simple programming exercise from my handbook.

I need to read four integers (author says input test will be introduced in next chapter) so step by step.

This is new source

Code:
me@my-pc:~/Desktop$ cat comparation.c 
// Compares four integers and prints which is greatest and which is smallest

#include <stdio.h>

int main(void) 
{
    int a, b, c, d;

    // Prompts user to input four integers and stores in variables a, b
    // c and d 

    printf("Enter four integers: ");
    scanf("%d %d %d %d", &a, &b, &c, &d);

    // to be the largest number among the numbers entered this number must 
    // be strictly greater than all numbers entered 
    // analogous for the smallest

    // Test for number stored in variable a

    if ( (a > b && a > c && a > d) || ( a < b && a < c && a < d) ) 
        printf("Greatest: %d\n", a);
    else    
        printf("Smallest %d\n", a);
    
    // Test for number stored in variable b
    
    if ( (b > a && b > c && b > d) || ( b < a && b < c && b < d) ) 
        printf("Greatest: %d\n", b);
    else    
        printf("Smallest %d\n", b);

    // Test for number stored in variable c 

    if ( (c > a && c > b && c > d) || ( c < a && c < b && c < d) ) 
        printf("Greatest: %d\n", c);
    else    
        printf("Smallest %d\n", c);

    // Test for number stored in variable d 

    if ( (d > a && d > b && d > c) || ( d < a && d < b && d < a) ) 
        printf("Greatest: %d\n", a);
    else    
        printf("Smallest %d\n", a);

    return 0;
}

Output

Code:
me@my-pc:~/Desktop$ ./test 
Enter four integers: 25 12 34 52
Smallest 25
Greatest: 12
Smallest 34
Greatest: 25

But I want to output like

Code:
Enter four integers: 1 2 3 4 
Greatest 4
Smallest 1

Smilie
# 6  
Old 01-17-2011
You want something like this -

Code:
$
$ cat -n comparison.c
     1  // Compares four integers and prints which is greatest and which is smallest
     2
     3  #include <stdio.h>
     4
     5  int main(void)
     6  {
     7      int a, b, c, d;
     8
     9      // Prompts user to input four integers and stores in variables a, b
    10      // c and d
    11
    12      printf("Enter four integers: ");
    13      scanf("%d %d %d %d", &a, &b, &c, &d);
    14
    15      // to be the largest number among the numbers entered this number must
    16      // be strictly greater than all numbers entered
    17      // analogous for the smallest
    18
    19      // Test for number stored in variable a
    20      if ( a > b && a > c && a > d )
    21          printf("Greatest: %d\n", a);
    22      else if ( a < b && a < c && a < d )
    23          printf("Smallest %d\n", a);
    24
    25      // Test for number stored in variable b
    26      if ( b > a && b > c && b > d )
    27          printf("Greatest: %d\n", b);
    28      else if ( b < a && b < c && b < d )
    29          printf("Smallest %d\n", b);
    30
    31      // Test for number stored in variable c
    32      if ( c > a && c > b && c > d )
    33          printf("Greatest: %d\n", c);
    34      else if ( c < a && c < b && c < d )
    35          printf("Smallest %d\n", c);
    36
    37      // Test for number stored in variable d
    38      if ( d > a && d > b && d > c )
    39          printf("Greatest: %d\n", d);
    40      else if ( d < a && d < b && d < c )
    41          printf("Smallest %d\n", d);
    42
    43      return 0;
    44  }
    45
$
$

That is, you perform the greatest/smallest test for each of the 4 numbers.

Code:
$
$ ./comparison
Enter four integers: 1 2 3 4
Smallest 1
Greatest: 4
$
$
$ ./comparison
Enter four integers: 18 44 7 31
Greatest: 44
Smallest 7
$
$

Alternatively, you may want to implement any of the standard sorting algorithms. (Although I suspect it might be a bit too overwhelming at this stage.)

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Can someone help me create this script(I'm a novice)

I've literally been handed this assessment for my course and the lecturer is of no help at all he's taught us virtually nothing would anyone be able to show me how the following script should look on Linux - For this assignment you have to write a Linux Shell Script which will allow a user to... (1 Reply)
Discussion started by: Andy_cyber
1 Replies

2. Shell Programming and Scripting

sed question from novice

My pipe delimited file is coming over with spurious “\” characters inserted into some alpha fields, which is causing the records to be split into 2. Eg Abc|def|10/11\ AAAA|xyz Lmn|opq|10/11BBBB|xyz etc etc I am having to go into vi, then enter / \ to... (5 Replies)
Discussion started by: malts18
5 Replies

3. Shell Programming and Scripting

Need help with a little script - novice

I am just learning unix and need some help. I am trying to display all of the files I have modified within the last 24 hours and sort them from the most recently modified. I can't figure it out.. I've been using a lot of ls and find commands. Here are some things I've tried: find . -mtime -1 |... (4 Replies)
Discussion started by: mredwin3
4 Replies

4. UNIX for Dummies Questions & Answers

NEW to Unix (novice)

Heya all Im just reading up on the solaris o/s and unix and i just have the following qustions 1) is the solaris o/s the same as Unix if not how are they different - i.e. are they different operating systems? 2) Can the Unix be loaded from CD without affectin windows o/s just like linux... (2 Replies)
Discussion started by: new214
2 Replies

5. Solaris

New to solaris (novice)

Heya all Im just reading up on the solaris o/s and have a few questions regarding it: 1) is the solaris of free to use/download? if yes where? 2) Can the solaris o/s be loaded from CD without affectin windows o/s just like linux can? 3) what are the hardware requirements for using... (2 Replies)
Discussion started by: new214
2 Replies

6. Shell Programming and Scripting

Unix scripting-Need help-NOVICE -PLEASE HELP

I really want to get into unix scripting,work with RS6000 -AIX. How do i get started,what books are good for beginners,i am very desperate I have no programming background but ready to scrafice all my time in learning .please help. PLEASE,PLEASE PLEASE ,HELP.... Any advice will realy... (2 Replies)
Discussion started by: Ghanaman
2 Replies

7. Shell Programming and Scripting

Problem for novice

Hi, I am observing a problem wiht my script. I tokk that part and executed from the command prompt. Below is the command the error. CAn any one suggest what should be done here. if ($cnt -lt $maxcnt) then echo deepu fi ksh: 0: not found Thanks in advance (16 Replies)
Discussion started by: deepaksamuel
16 Replies

8. Linux

Hi I M Novice User

hi everyone i m a novice user . just want to know how to use this website and also learn unix from the basics. can any one help me please.... (2 Replies)
Discussion started by: MSK
2 Replies

9. Programming

novice student needs help

Help! I am very stuck!!! I have to produce a practical implementation of ONC RPC for an assignment and I do not know where to start. I hve done much searching on sun's site but everything is too complicated for someone with my limited knowledge. I only know the very basic unix commands and have... (1 Reply)
Discussion started by: karen79
1 Replies

10. Linux

Question From a Novice

HELLO GUYS, How u all guys doing?Recently I brought a system and installed Red Hat Linux in it. I have also got a network card. My question is can i connect NIC to my system and use it as a client? I really don't want to buy another system. I want to use this computer/system both as server and... (0 Replies)
Discussion started by: cyno
0 Replies
Login or Register to Ask a Question