Cannot find out my mistake

hello, i try to make an programm to sort my (a,b,c,d) array descending;
this is what i've done so far.
please help me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<algorithm>
using namespace std;
const int nSize = 4;
int a,b,c,d;
int anArray[nSize] = { a, b, c, d };
{   
    cout<<"Give 4 numbers:\n";
    cin>>a>>b>>c>>d;
for (int nStartIndex = 0; nStartIndex < nSize; nStartIndex++)
{
    int nLargestIndex = nStartIndex;
    for (int nCurrentIndex = nStartIndex + 1; nCurrentIndex < nSize; nCurrentIndex++)
    {
        if (anArray[nCurrentIndex] > anArray[nLargestIndex])
            nLargestIndex = nCurrentIndex;
    }
    cout<<"Array is: "<<swap(anArray[nStartIndex], anArray[nLargestIndex]);
}
system("pause");
}


my error on compiling is :

7 expected unqualified-id before '{' token

7 expected `,' or `;' before '{' token
Last edited on
maybe on line 7 you meant to write
int main(){
if i do that, i get a lot of errors like:

15 ")) << std::swap [with _Tp = int](((int&)((+(((unsigned int)nStartIndex) * 4u)) + ((int*)(&anArray)))), ((int&)((+(((unsigned int)nLargestIndex) * 4u)) + ((int*)(&anArray)))))'

note candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]

note std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]

and so on ...

i am trying to make the program to show me the New Order in the array.
thats why i typed cout<<"Array is: "<<swap(anArray[nStartIndex], anArray[nLargestIndex]); in stead of swap(anArray[nStartIndex], anArray[nLargestIndex]);

i just dont know how to make it show me the new order of numbers.
please help me ( is a homework. i need to solve it until tomorrow. )
That is because you have other errors in your code.

1. Line 7 we have already spoken about.

2. By the way the following makes no sense really
1
2
3
4
int a,b,c,d;
int anArray[nSize] = { a, b, c, d };

cin>>a>>b>>c>>d; //This does not get anything into the array 


3. This line is incorrect because you are trying to display the result of the swap function.
The swap function is a void function.
 
cout<<"Array is: "<<swap(anArray[nStartIndex], anArray[nLargestIndex]);
so ... im very confuse , what should the program look like than ???
can you give me a clue about how the hell can i do a program to show me 4 given numbers in descending order ???
I can see you are confused;

Start with this and tidy it up as you see fit:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include<algorithm>

using namespace std;

const int nSize = 4;
int anArray[nSize] = {0};

int main()
{   
    
    cout<<"Give 4 numbers:\n";
    
    //we only want 4 numbers - so I didn't bother wih a loop
    cin>>anArray[0]>>anArray[1]>>anArray[2]>>anArray[3];
    
    
    for (int nStartIndex = 0; nStartIndex < nSize; nStartIndex++)
    {
        int nLargestIndex = nStartIndex;
        for (int nCurrentIndex = nStartIndex + 1; nCurrentIndex < nSize; nCurrentIndex++)
        {
            if (anArray[nCurrentIndex] > anArray[nLargestIndex])
                nLargestIndex = nCurrentIndex;
       }
           swap(anArray[nStartIndex], anArray[nLargestIndex]);
      
    }

    //print out the  numbers
     for (int count =0; count < nSize; ++count)
     {
         cout << anArray[count] << endl;
     }
    
    system("pause");//we'll live with this 
}
i tryed your program ( oh yeah, thx is awsome, i had some mistakes :D) it gives me 2 "errors" at compile:

[Linker error] undefined reference to `__cpu_features_init'

ld returned 1 exit status
i tryed this QuickSort too, but exactly same errors ( it might be, because i use DevC++ 4.9.9.2 ??)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void quickSort(int arr[], int left, int right) {
      int i = left, j = right;
      int tmp;
      int pivot = arr[(left + right) / 2];
 
      /* partition */
      while (i <= j) {
            while (arr[i] < pivot)
                  i++;
            while (arr[j] > pivot)
                  j--;
            if (i <= j) {
                  tmp = arr[i];
                  arr[i] = arr[j];
                  arr[j] = tmp;
                  i++;
                  j--;
            }
      };
 
      /* recursion */
      if (left < j)
            quickSort(arr, left, j);
      if (i < right)
            quickSort(arr, i, right);
}

yes - this error "undefined reference to `__cpu_features_init' " is related to your DevC++ setup
(see Helios comments on this thread http://www.cplusplus.com/forum/beginner/21858/ )
Last edited on
wow that was helpful !! THANKS ! :D
Topic archived. No new replies allowed.