GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Nov-2008, 23:52
PowerHouse PowerHouse is offline
New Member
 
Join Date: Nov 2008
Posts: 26
PowerHouse is on a distinguished road
Unhappy

Sorting an Array using a function


Hello all,
I have a program that I am currently working on that is supposed to fill an array from a text file, then (after finding min and max value of array) is supposed to sort the values from lowest to highest. All was going well until I hit a brick wall. Apparently the void function Sort (shown near bottom under sort function comment) is incorrectly being called upon or something. I am not sure, as I'm fairly new to C language. The error code i receive is: [31 F:\main1.cpp invalid use of void expression]. I THINK that's the only thing that is stopping the program from running, however i could be wrong. Okay, here is the code, please help me.
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

int main (void)
{
    int k,t;
    double  x[10],y[10];
    double min_x(double x[],int t);
    double max_x(double x[],int t);
    void sort(double x[],int t);
    FILE *input;
    input = fopen("F:\\eng c projects\\input1.txt","r");
    if (input == NULL)
      {
       printf("The file failed to open\n");
       }
    else
     {
      printf("File Opened\n");
      for(k=0;k<=9;k++)
       {
        printf("Scanning......\n");
        fscanf(input,"%lf",&x[k]);
        printf("Value of array is:\n %5.2f\n",x[k]);
       }
      printf("The min value of array X is:\n %5.2f\n", min_x(x,t));
      printf("The max value of array X is:\n %5.2f\n", max_x(x,t));
      printf("The X array sorted:\n"); 
      for(k=0;k<=9;k++)
      {
       printf("%5.2f\n", sort(x,t));
      
      } 
      
      
  system("Pause");
  return(0);
}

// Min Function

double min_x(double x[],int t)
{
 int w;
 double min_x;

 min_x=x[0];
 for(w=1;w<=9;w++)
  {
   if (x[w] < min_x)
    {
     min_x = x[w];
    }
  }
  return min_x;
}

// Max Function
double max_x(double x[],int t)
{
 int w;
 double max_x;

 max_x=x[0];
 for(w=1;w<=9;w++)
  {
   if (x[w] > max_x)
    {
     max_x = x[w];
    }
  }
  return max_x;
}

// Sort Function
void sort(double x[], int t)
{
     int k,j,m;
     double hold;
     for(k=0;k<=t-2;k++)
      {
       m=k;
       for(j=k+1;j<=t-1;j++)
        { 
         if(x[j]<x[m])
          {
           m=j;
          }
         }
        hold=x[m];
        x[m]=x[k];
        x[k]=hold;
       }
     return;
}          
  #2  
Old 20-Nov-2008, 07:21
Blake's Avatar
Blake Blake is offline
Member
 
Join Date: Nov 2005
Posts: 255
Blake is a jewel in the roughBlake is a jewel in the rough

Re: Sorting an Array using a function


The problem is here:

CPP / C++ / C Code:
      for(k=0;k<=9;k++)
      {
       printf("%5.2f\n", sort(x,t));

      }

You are calling the sort function 10 times (once on each iteration of the for loop), and attempting to print its output each time, which is void. You should sort the array once, and then loop through the array and print each element, like this:

CPP / C++ / C Code:
      sort(x,t);
      for(k=0;k<=9;k++)
      {
       printf("%5.2f\n", x[k]);
      }

You're not initializing the variable t either. You'll need to give it a value, or none of this will work.

You're also missing a closing curly brace in if-else statement.

Also, if you're planning on doing bigger sorting tasks, you should consider a more efficient sorting algorithm, such as merge-sort. The difference would be negligible when the array only has ten elements, but it can make a big difference for larger arrays,
__________________
www.blake-foster.com
  #3  
Old 20-Nov-2008, 10:19
PowerHouse PowerHouse is offline
New Member
 
Join Date: Nov 2008
Posts: 26
PowerHouse is on a distinguished road

Re: Sorting an Array using a function


[REVISED]
Blake,
Thank you so much, that was some solid advice. I have implemented the changes in the code, however, i believe I have made a mistake somewhere, as the array was not sorted, BUT the program did run. I have created a function to normalize the array, however i keep receiving this error:[35 C:\Dev-Cpp\main7.cpp cannot convert `double (*)(double*, int)' to `double' for argument `2' to `void norm(double*, double, double)' ] The complier points to line 35 (will be indicated with ***) I assume its from calling upon another function that uses integers in its operation. I BELIEVE a cast operator might be necessary to allow this function to work, but like I said I am a rookie. The output i am looking for is having the original array next to the sorted array. I am not sure where to begin, but i do know that the order of the first array must be preserved. Should i do this by setting a variable equal to the array within a loop? Here is the code i have so far(revised):
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

int main (void)
{
    int k,t=0;
    double  x[10],y[10],s;
    double min_x(double x[],int t);
    double max_x(double x[],int t);
    void norm(double y[],double min_x,double max_x);
    void sort(double y[], int t);
    FILE *input;
    input = fopen("E:\\eng c projects\\input1.txt","r");
    if (input == NULL)
      {
       printf("The file failed to open\n");
      }
    else
     {
      printf("File Opened\n");
      printf("Scanning......\n");
      for(k=0;k<=9;k++)
       {
        fscanf(input,"%lf",&x[k]);
        printf("x[%i]: %5.2f\n",k,x[k]);
       }
      printf("Copying Array..\n");
      for (k=0;k<=9;k++)
      {
        y[k]=x[k];
      } 
      printf("The min value of array X is:\n %5.2f\n", min_x(x,t));
      printf("The max value of array X is:\n %5.2f\n", max_x(x,t));
      printf("The Y array is:\n"); 
*** norm(y,min_x,max_x);
      sort(y,t);
      for(k=0;k<=9;k++)
      {
       printf("%5.2f\n",y[k]);
      } 
    }  
      
  system("Pause");
  return(0);
}

// Min Function

double min_x(double x[],int t)
{
 int w;
 double min_x;

 min_x=x[0];
 for(w=1;w<=9;w++)
  {
   if (x[w] < min_x)
    {
     min_x = x[w];
    }
  }
  return min_x;
}

// Max Function
double max_x(double x[],int t)
{
 int w;
 double max_x;

 max_x=x[0];
 for(w=1;w<=9;w++)
  {
   if (x[w] > max_x)
    {
     max_x = x[w];
    }
  }
  return max_x;
}

//Normalization
void norm(double y[],double min_x,double max_x)
{
  int k;
  double n;
  for(k=0;k<=9;k++)
  {
   y[k] = (y[k]- (float)min_x)/((float)max_x-(float)min_x);
  }
  return;
}
// Sort Function
void sort(double y[], int t)
{
     int k,j,m;
     double hold;
     for(k=0;k<=t-2;k++)
      {
       m=k;
       for(j=k+1;j<=t-1;j++)
        { 
         if(y[j]<y[m])
          {
           m=j;
          }
         }
        hold=y[m];
        y[m]=y[k];
        y[k]=hold;
       }
     return;
}          

Help would be much appreciated, sorry for the revision, thanks for reading.
  #4  
Old 20-Nov-2008, 22:22
PowerHouse PowerHouse is offline
New Member
 
Join Date: Nov 2008
Posts: 26
PowerHouse is on a distinguished road

Re: Sorting an Array using a function


Well i finally got it! Thank you for your help Blake! Until next time !
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
run script command on ns2.26 newbie06 Computer Software Forum - Linux 65 19-Aug-2009 08:50
Getting a line error in register oggie MySQL / PHP Forum 5 13-Apr-2008 17:16
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 17:19
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 20:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:26.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.