Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Probably a trivial C question

Status
Not open for further replies.

tanky321

Member level 1
Joined
Jan 2, 2008
Messages
32
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,546
Im trying to right a program that is given 5 integers, it then sorts the integers in numerical order. Catch is it has to use if else statements or loops.

Its for a class im in, and im at a complete loss. Heres what I have so far, only for the first two numbers, but it doesnt work. Any tips or help would be appreciated.


void order(int a, int b, int c, int d, int e)
{ int num_1=0;
int num_2=0;

if(a<b && a<c && a<d && a<e)
{num_1=a;
a=0;}
else if(b<a && b<c && b<d && b<e)
{num_1=b;
b=0;}
else if(c<a && c<b && c<d && c<e)
{num_1=c;
c=0;}
else if(d<a && d<b && d<c && d<e)
{num_1=d;
d=0;}
else
{num_1=e;
e=0;}
if (a<b && a<c && a<d && a<e && a!=num_1 && a!=0)
{num_2=a;
a=0;}
else if(b<a && b<c && b<d && b<e && b!=num_1 && b!=0)
{num_2=b;
b=0;}
else if(c<a && c<b && c<d && c<e && c!=num_1 && c!=0)
{num_2=c;
c=0;}
else if(d<a && d<b && d<c && d<e && d!=num_1 && d!=0)
{num_2=d;
d=0;}
else if(e<a && e<b && e<c && e<d && e!=num_1 && e!=0)
{num_2=e;
e=0;}



printf("%d %d %d\n\n\n", num_1,num_2);

}
 

there are many ways of doing this. Much easier to read/write when using a loop.
An easy way to do it is to find the min of all five, then store it in "num_1", then find the min of the remaining four, store, and repeat until done.
 

Instead of using five separate integer variables, it would be easier to put the input data into a five-element array, and then process it with a loop.

Try searching Google for "sorting algorithms", and you will find descriptions of various techniques. For example, the "bubble sort" is probably the simplest of all, although it's relatively inefficient:
https://en.wikipedia.org/wiki/Sorting_algorithm
 

The sorting and arrays sound great, but I cant use them. Im pretty much restricted to Loops and if else statements.

I got the program to working using a for loop, but it wont work if I have duplicate entries. Il post up my program in a second, just need to open my laptop.


Thanks guys.
 

use state variable (bitmap) to store whether vars are printed or not . Start to find highest one , once found - print it and mark its state bitmap variable as processed then start again in loop and skip those vars whose states are marked as sorted. Then no array will be needed.
 

This is what I have, i use 1000000 so it will kind of exclude that variable. But it ends up screwing me in the long run. I would love to use that bitmap function, but we haven't covered that, and he wont let us use it.



void order(int a, int b, int c, int d, int e)
{ int num_1=0;
int count;

for(count=0;count<5;count++)
{
if(a<b && a<c && a<d && a<e)
{num_1=a;
a=1000000;}
else if(b<a && b<c && b<d && b<e)
{num_1=b;
b=1000000;}
else if(c<a && c<b && c<d && c<e)
{num_1=c;
c=1000000;}
else if(d<a && d<b && d<c && d<e)
{num_1=d;
d=1000000;}
else if(e<a && e<b && e<c && e<d)
{num_1=e;
e=1000000;}

printf(" %d ",num_1);
}
}
 

Code:
// this is a quick and dirty implementation of a bubble sort. It sorts in ascending
// order. I used devcpp 

#include <cstdlib>
#include <stdio.h>
#include <conio.h>

using namespace std;
void SortIt(int *);

int main(int argc, char *argv[])
{
   int a[] = {1123, 675, 435, 33, 456};  ;array of numbers to be sorted
   int i;
   
   for (i = 0; i <= 4; i++)                     ;output unsorted numbers
      printf("%d  ", a[i]);
      
   SortIt(a);   
    
   printf("\n\n"); 
   
   for (i = 0; i <= 4; i++)                    ;output sorted numbers
      printf("%d  ", a[i]);
      
    getch(); 
    system("PAUSE");
    return EXIT_SUCCESS;
}


void SortIt(int *a)
{
   int  i, flag, temp, count;

   flag = 1;   // 0 will indicate no swaps were made, sorting is done
   count = 0;  // just used for debugging, it will 
                   // end an infinite loop condition. 

   while (flag !=  0 & count < 2000)
   {
      flag = 0; 
      count++;
      for (i = 0; i <= 3; i++)
      {
         if (a[i] > a[i+1] )     //test for greater than condition
         {
            temp = a[i];        // and swap if necessary
            a[i] = a[i+1];
            a[i+1] = temp;
            flag = 1;             //set flag to indicate still some sorting to do
         } // end of it
       }  // end of for
     } // end of while
} // end of SortIt

This is what the output of the program will look like:
1123 675 435 33 456

33 435 456 675 1123
 

Your professor's rules don't leave you much to work with.
How about a reentrant solution?
Code:
void order(int a, int b, int c, int d, int e)
{
  if (a > b)
    order(b, a, c, d, e);
  else if (b > c)
    order(a, c, b, d, e);
  else if (c > d)
    order(a, b, d, c, e);
  else if (d > e)
    order(a, b, c, e, d);
  else
    printf("%d %d %d %d %d\n", a, b, c, d, e);
}
 

echo47 said:
Your professor's rules don't leave you much to work with.
How about this?
Code:
void order(int a, int b, int c, int d, int e)
{
  if (a > b)
    order(b, a, c, d, e);
  else if (b > c)
    order(a, c, b, d, e);
  else if (c > d)
    order(a, b, d, c, e);
  else if (d > e)
    order(a, b, c, e, d);
  else
    printf("%d %d %d %d %d\n", a, b, c, d, e);
}

That looks good, but the only thing is that we havent used the Order function yet. I actually did something to my for loop program and it worked. I changed all of the "<" to "<=" and it works like a charm. I know its not the right way to do it, but it will work!


I really thank all of you for your help, Its opened my eyes up!



Andrew
 

Look again! 'order' isn't some library function. The 'order' function calls itself.

Oops, earlier I said "reentrant ". The correct term is "recursive".
 

echo47 said:
Look again! 'order' isn't some library function. This function calls itself.

Oops, earlier I said "reentrant ". The correct term is "recursive".


oops i didnt even notice that! Thats pretty ingenious, but he would know I didnt come up with that! I dont have the experience to come up with that.


Thanks alot! I really appreciate it!!
 

const int size =5;
int temp;
int x[size]={2, 6, 1, 7, 9};
for(int i=0;i<size-1;i++)
for(int j=i+1;j<size;j++)
if(x<x[j]){temp=x;x=x[j];x[j]=temp;}
for(i=0;i<5;i++)cout<<x;
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top