2 Replies - 575 Views - Last Post: 08 November 2011 - 05:09 PM Rate Topic: -----

#1 rvbsanjose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-October 11

Newbie CS student - Did I use the if/else statement correct?

Posted 08 November 2011 - 04:36 PM

Hey guys,

Back at this again with another question from this amazing community. Before I get into my question, I just want to thank everyone again for taking what seems to be such easy answers to my questions for many of you and helping me out greatly to where I have been able to excel in my class thus far! Much appreciated!!!

We just started chapter 5 which deals with selection and my lab assignment I had to use at least one function in addition to main, must use selection, and must use the indirection pointer.

My question is did I use the if/else statement correctly? The lab calls for you to do nothing with the integers if they are already placed in the smallest to largest order. Variable e & f both evaluate to 6 and the lab says to do nothing if they are already smallest to largest. So I typed my if/else statement for variable e & f as


if (e <= f)
        printf ("\t Result 3: %3d\t%3d\n", e, f);
    else
        printf ("\t Result 3: %3d\t%3d\n", e, f);



Is that above if/else statement really saying to do nothing with the order? Technically it's still true/false isn't it because I'm saying if it's not this then do this but that doesn't really seem like doing nothing to the order if it's already in smallest to largest order.

The program seems to run perfect but I wanted to know from this community if I'm going about the coding process correctly or did I bloat it when where it isn't necessary. I've included the entire program I wrote for the visuals. Any advice on what I can do to make it better would be greatly appreciated.


/*
 
 Develop a program to meet the following requirements:
 
 1.  Must use at least one function in addition to main
 2.  Must use selection
 3.  Must use deference/indirection (the * operator)
 
 Take two integers as input, compare them and put them in smallest to largest order. 
 
 If they are not in smallest to largest order, use an exchange function to swap their order.  If they are already in order, do nothing.
 
 Output both integers in smallest to largest order. Use the following integer pairs as input (you will want to use more).
 
 7      12
 18     3
 6      6
 -12    0
 
*/

#include <stdio.h>

//Function Declarations

void swap     (void);
void original (int* a, int* b, int* c, int* d, int* e, int* f, int* g, int* h, int* i, int* j, int* k, int* l);

int main (void) //Function "Main" Begins

{
    
    //Local Declarations
    
    int a;
    int b;
    int c;
    int d;
    int e;
    int f;
    int g;
    int h;
    int i;
    int j;
    int k;
    int l;
    
    original (&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l); //calling function "Original"
    swap     ();                                               //calling function "Swap"
    
    return 0; //Return control back to operating system
    
} //Function "Main" Ends

void original (int* a, int* b, int* c, int* d, int* e, int* f, int* g, int* h, int* i, int* j, int* k, int* l) //Function "Original" Begins

{
    
    //Local Declarations
    
    *a = 7;
    *b = 12;
    *c = 18;
    *d = 3;
    *e = 6;
    *f = 6;
    *g = -12;
    *h = 0;
    *i = 17;
    *j = 9;
    *k = 49;
    *l = 23;
    
    //Statements
    
    printf ("\n==============================\n");
    printf ("Original Order of the Integers\n");
    printf ("==============================\n");
    printf ("\n\t Result 1: ");
    printf ("%3d\t%3d\n", *a, *B)/>;
    printf ("\t Result 2: ");
    printf ("%3d\t%3d\n", *c, *d);
    printf ("\t Result 3: ");
    printf ("%3d\t%3d\n", *e, *f);
    printf ("\t Result 4: ");
    printf ("%3d\t%3d\n", *g, *h);
    printf ("\t Result 5: ");
    printf ("%3d\t%3d\n", *i, *j);
    printf ("\t Result 6: ");
    printf ("%3d\t%3d\n", *k, *l);
    
    return;
    
} //Function "Original" Ends

void swap (void) //Function "Swap" Begins

{
    
    //Local Declarations
    
    int a;
    int b;
    int c;
    int d;
    int e;
    int f;
    int g;
    int h;
    int i;
    int j;
    int k;
    int l;
    
    a = 7;
    b = 12;
    c = 18;
    d = 3;
    e = 6;
    f = 6;
    g = -12;
    h = 0;
    i = 17;
    j = 9;
    k = 49;
    l = 23;
    
    printf ("\n==============================\n");
    printf ("  Swap Order of the Integers\n");
    printf ("==============================\n");
    
    if (a <= B)/>
        printf ("\n\t Result 1: %3d\t%3d\n", a, B)/>;
    else
        printf ("\t Result 1: %3d\t%3d\n", b, a);
    
    if (c <= d)
        printf ("\t Result 2: %3d\t%3d\n", c, d);
    else
        printf ("\t Result 2: %3d\t%3d\n", d, c);
    
    if (e <= f)
        printf ("\t Result 3: %3d\t%3d\n", e, f);
    else
        printf ("\t Result 3: %3d\t%3d\n", e, f);
    
    if (g <= h)
        printf ("\t Result 4: %3d\t%3d\n", g, h);
    else
        printf ("\t Result 4: %3d\t%3d\n", h, g);
    
    if (i <= j)
        printf ("\t Result 5: %3d\t%3d\n", i, j);
    else
        printf ("\t Result 5: %3d\t%3d\n", j, i);
    
    if (k <= l)
        printf ("\t Result 6: %3d\t%3d\n\n", k, l);
    else
        printf ("\t Result 6: %3d\t%3d\n\n", l, k);
    
    return;
    
} //Function "Swap" Ends

/*
 
 Sample output of my program for Lab # 4
 
 ==============================
 Original Order of the Integers
 ==============================
 
 Result 1:   7	 12
 Result 2:  18	  3
 Result 3:   6	  6
 Result 4: -12	  0
 Result 5:  17	  9
 Result 6:  49	 23
 
 ==============================
 Swap Order of the Integers
 ==============================
 
 Result 1:   7	 12
 Result 2:   3	 18
 Result 3:   6	  6
 Result 4: -12	  0
 Result 5:   9	 17
 Result 6:  23	 49
 
*/




Is This A Good Question/Topic? 0
  • +

Replies To: Newbie CS student - Did I use the if/else statement correct?

#2 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 450
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Newbie CS student - Did I use the if/else statement correct?

Posted 08 November 2011 - 04:54 PM

Reading the assignment, I don't think you fully understand what they expect.

Quote

Take two integers as input


I'm pretty sure they expect you to run your program in a console and ask the user for 2 integers. Take a look at the scanf function for C or cin for C++.

Quote

If they are not in smallest to largest order, use an exchange function to swap their order.


This makes me suspect that they don't just want you to print them in the right order but actually switch the contents of the variables.

Basically I think what they want in pseudo code so it isn't too spoilerish.

void swap(var1, var2 (use indirection here so you can modify variables passed)) {
    //swap var1 and var2
}

int main() {
    //read var1, var2 from input
    if (var1 > var2)
        swap(var1, var2)

    print var1
    print var2 
}





This post has been edited by Karel-Lodewijk: 08 November 2011 - 05:01 PM

Was This Post Helpful? 2
  • +
  • -

#3 rvbsanjose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-October 11

Re: Newbie CS student - Did I use the if/else statement correct?

Posted 08 November 2011 - 05:09 PM

View PostKarel-Lodewijk, on 08 November 2011 - 03:54 PM, said:

Reading the assignment, I don't think you fully understand what they expect.

Quote

Take two integers as input


I'm pretty sure they expect you to run your program in a console and ask the user for 2 integers. Take a look at the scanf function for C or cin for C++.

Quote

If they are not in smallest to largest order, use an exchange function to swap their order.


This makes me suspect that they don't just want you to print them in the right order but actually switch the contents of the variables.

Basically I think what they want in pseudo code so it isn't too spoilerish.

void swap(var1, var2 (use indirection here so you can modify variables passed)) {
    //swap var1 and var2
}

int main() {
    //read var1, var2 from input
    if (var1 > var2)
        swap(var1, var2)

    print var1
    print var2 
}






Ah I totally missed the input as a requirement. You probably just saved me 10% on my lab. Thanks so much for catching that! I see where my problems areas are. Much appreciated for typing up an example. Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1