Sorting arrays, please help me

I came here for help with writing my program. Instead I got all sorts of rude comments. I took Aramil's suggestion and indented my program. Still people are commenting on that instead of helping me with my code. This is the fifth program I have ever written, I am not used to programming yet. Thank you Peter, for trying to help me.
Last edited on
can I make a suggestion? Its hard to read. I would suggest cleaning up the code by using ides. They help it look neat. Also next time you post code there is a button that has <>. Click on it and post your code in it.
lol, You can write neat code without IDEs :D

All you output after the input is the result from list_find. If you want to call list_sort and list_display make proper function calls. Now they are just function declarations.
no i understand that you don't need an ide for clean code. I originally used the note pad equivalent in linux with the tab button. What i mean to say is that an ide takes away the part of cleaning up the code, not only allowing you to focus more on the writing, but also making it easier to read if there is a problem.
Even vi will do that for you. That is not a question of IDE, it's a question of habit. I really can't believe people actually post code like this on forums and expect other people to understand it.
OP:

I can't display the sorted version of my array. I tried to call the functions and now I have 7000 errors when I run the program.


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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

//Outputs both unsorted and sorted array using list_display. 
   void list_display (const int [], int );
//average value of the numbers. 
   float list_average (const int list[], int l);
//highest value of the numbers. 
   int list_max (const int list[], int l); 
//smallest value of the numbers. 
   int list_min (const int list[], int l); 
//count the number of occurrences of some key value in list 
   int list_find(const int list[], int l, int number);
//sorts array in ascending order using a swap sort. 
   void list_sort(int list[], int l); 


int main ()
   {
   const int l=20; 
// 20 integer numbers between 1 and 20 are stored in an array called list
   int list[l] = {1,4,6,8,9,10,3,5,7,9,1,9,2,8,3,7,4,5,6,7};
   int number; 
   list_display (list,l);
   cout <<list_average(list,l)<<endl; 
   cout<<list_min(list,l)<<endl; 
   cout<<list_max(list,l)<<endl; 
// key value to find occurences 
   cin>>number;  
   cout<<list_find(list,l,number)<<endl; 
   cout<<list_sort (list,l)<<endl; 
   cout<<list_display(list,1)<<endl;
   return 0; 
   }

          float list_average (const int list[], int l)
       {
          float sum=0; 
          for(int i = 0; i<l; ++i) sum+=list[i]; 
          return sum/l; 
       }
       void list_display (const int list[], int l)
       {

       for(int i = 0; i<l; ++i) 
	   cout << list[i]<<endl; 
       }

   int list_max (const int list[], int l)
   {
	   int largest=list[0]; 
	   for(int i = 0; i<l; ++i) 
	  {
		if (list[i]> largest)
		  {
	                  largest=list[i]; 
		  }
            }

	return largest; 
    }

   int list_min (const int list[], int l)
   {
	int smallest=list[0]; 
	   for(int i = 0; i<l; ++i) 
       {
                        if (list[i]< smallest)
                       {
                        smallest=list[i]; 
                        }
        }

	return smallest; 
    }

   int list_find (const int list[], int l, int number)
   {
   int sum=0; 
               for(int i = 0; i<l; ++i) 
	 {
		 if (list[i]== number)
		  { 
		    sum+=1; 
                     }
	 }
   return sum; 

   } 
   void list_sort (int list[], int l)
   {
    int x; 
	  for(int i=0; i<l-1; ++i)
	    for (int j=i+1; j<l;j++)
	   {
		   if (list[i]<list[j]) 
		   {
		          x=list[i];
	                   list[i]=list[j];
	                   list[j]=x; 
			
		   }
	   }
	
   }
Topic archived. No new replies allowed.