3 Replies - 749 Views - Last Post: 25 February 2013 - 04:07 AM Rate Topic: -----

#1 realnoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 14-October 12

My first program in C

Posted 25 February 2013 - 12:27 AM

Hello:
This is my first program in C, I'm having trouble with doing this is assignment, I have started something, but I do not know what I'm doing wrong.

Pass by Reference
Write a program that sorts three user inputted integers.

This program should:

1.Call a function which does the sorting.
2.Have ONLY local variables!!!
3.Do NOT use an array.
4.The function should NOT use the return statement to return any values.
5.The function will
&νβσπ;be passed, BY REFERENCE, the three values to be sorted
&νβσπ;pass back, BY REFERENCE, the sorted values
&νβσπ;NOTHING else will be passed to, or from the function.

6.main( ) will:
&νβσπ;ask the user for three values to sort
&νβσπ;print the values of the three variables in the inputted order (label them with their names)
&νβσπ;call the function
&νβσπ;print the three variables, in the sorted order (label them again)

my attempt:
#include<stdio.h>

void sort (int*, int*, int*);  // prototype

void sort(int *var1, int *var2, int *var3)
{
  //int var1, var2, var3;
 
  
  
  if ( *var1 > *var2 || *var1 > *var3){
       printf("The value 1 = %d\n", var1);
     }
  else if ( *var2 > *var1 || *var2 > *var3){
     printf("The value 2 = %d\n", var2);
     
     }
  else if ( *var3 > *var1 || *var3 > *var2){
       printf("The value 2 = %d\n", var3);
       }                        
}

int main ()
{
  int var1, var2, var3;
  printf("Enter three integers to be sorted\n");
  scanf("%d", &var1);
  scanf("%d", &var2);
  scanf("%d", &var3);
  
  printf("Integer that you have entered is %d\n", *var1 );
  
  printf(sort);
}


sample output:

The three values to be sorted are:
value 1 = 56 value 2 = 87 value 3 = 45

The sorted values are:
value 1 = 45 value 2 = 56 value 3 = 87

Is This A Good Question/Topic? 0
  • +

Replies To: My first program in C

#2 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,903
  • Joined: 12-December 12

Re: My first program in C

Posted 25 February 2013 - 12:37 AM

printf("Integer that you have entered is %d\n", *var1 );

printf(sort);

I don't use C/C++ but I assume that you don't need the de-referencing * because var1 is already an integer(?). Also, this line will only print the last of the three values that you've input.

More importantly, you are not passing the three values to your sort() function.
Was This Post Helpful? 0
  • +
  • -

#3 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 410
  • View blog
  • Posts: 1,449
  • Joined: 14-September 12

Re: My first program in C

Posted 25 February 2013 - 01:15 AM

Well, you need to call your sort() function using 3 parameters...you don't have to do print(sort) as already the sort method has print statements for the respective cases.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: My first program in C

Posted 25 February 2013 - 04:07 AM

You might want to read the function tutorials in my signature.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1