i am having two problems with my code. the first error message highlighted in green says i need to return a value instead of what i have written down. and the line highlighted in red says that my value is not being ignored. can anyone help? I am totally lost. here is my code:

#include <iostream>
using namespace std;
const int MAXELS = 1000;
void  readList(int nums [], int size);
void printList( int list[], int size);
void printbackward(int list[], int size);
void findmaxLocation (int list[], int size);
void swap(int&x, int&y);
void sort(int list[], int size);
 
int main()
{
   int j;
     
   int theList[MAXELS];
  readList (theList, 5); 
  printList (theList, 5);
  printbackward (theList, 5);
  
  sort (theList, 5);
 
  cin >> j;
  return 0;
    
} 

void  readList(int nums [], int size)
{
      int i;
      
      for(i=0; i < size; i++)
      
      cout << "Enter the " << i + 1 << " number" << endl;
      
      cin >> nums[i];
      
}
void printList( int list[], int size)
{
     int i;
    
     
     for(i = 0; i < size; i++)
     
     cout << "List[" << i << "] is" <<list[i] << endl;
    
} 
void printbackward(int list[], int size)
{
     int i;
     
     for(i = (size - 1); i >= 0; i--)
     
     cout << list[i-1];
}
void findmaxLocation (int list[], int size)
{ 
     int max, maxlocation;
     
     max = list[0];
     maxlocation = 0;
     
     for(int i = 1; i < size; i++)
     
     if (max < list[i])
     {
             max = list[i];
             maxlocation = i;
     }
     return maxlocation;
}
void swap(int&x, int&y)
{ 
     int temp;
     temp = x;
     x = y;
     y = temp;
}
     
void sort(int list[], int size)
{
     int unsorted_size = size;
     int indexofMax;
     
     while (unsorted_size > 1)
     {
           indexofMax = findmaxLocation (list, unsorted_size);
           swap (list[indexofMax], list[unsorted_size-1]);
           unsorted_size--;
     }
}

Recommended Answers

All 3 Replies

In first function void findmaxLocation (int list[], int size)
you can't return a value.Because the functions, which types are defined void can't return a value.
Change it from void to int (int findmaxLocation (int list[], int size)

void findmaxLocation (int list[], int size)
 { 
      int max, maxlocation;
      
      max = list[0];
      maxlocation = 0;
      
      for(int i = 1; i < size; i++)
      
      if (max < list[i])
      {
              max = list[i];
              maxlocation = i;
      }
    
      
return maxlocation;        
   }

And second function's error grow out of first function.Because you're trying to return a value but you can't.So in the line which you've highlited in red the program can't assign a value to the indexofMax

void sort(int list[], int size)
 {
      int unsorted_size = size;
      int indexofMax;
      
      while (unsorted_size > 1)
      {

           indexofMax = findmaxLocation (list, unsorted_size);

         swap (list[indexofMax], list[unsorted_size-1]);
            unsorted_size--;
      }

>> the first error message highlighted in green says i need to return a value instead of what i have written down.
Yeah, when you promise something (void findmaxLocation() ) and then break your promise ( return maxlocation; ) compiler has gotta give you an error.

>> and the line highlighted in red says that my value is not being ignored.
THat's coz there is no conversion available from void to int. :).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.