help with sorting array values

help with a function. I'm trying to use the function bubblesort to sort my values in the array positive_num. can somebody please tell me what I'm doing wrong? thanks!

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

void get_data(int);
void bubblesort(int[], int);

int main ()
{
  int list, i = 0;

  get_data(list);

  return 0;
}


void get_data(int list)
{
  string filename, filename2;
  ifstream infile;
  ofstream outfile;
  int index = 0, count = 0, positive_num[30];
  double average = 0.0, sum = 0.0;

  cout << "Please enter the name of the input file." << endl; //prompt user for input file                                                                                                                                                                                                                                   
  cin >> filename;
  infile.open(filename.c_str());   //opens input file                                                                                                                                                                                                                                                                        

  cout << "Please enter the name of the output file." << endl; //prompt user for output file                                                                                                                                                                                                                                 
  cin >> filename2;
  outfile.open(filename2.c_str()); //opens output file                                                                                                                                                                                                                                                                       

  while(infile >> list)
    {
      if(list < 0){
        count++;
        sum = sum + list;
      }
      else
        {
        positive_num[index] = list;
        index++;
        }
    }
  average = sum / count;

  infile >> list;
  outfile << "Kevin Hunt Section #1010 Assign #4" "\n\n";
  outfile << "Negative Values" "\n";
  outfile << "Count: " << right << setw(22) <<  count << "\n";
  outfile << "Sum: " << fixed << setprecision(3) << right << setw(24) << sum << "\n";
  outfile << "Average: " << fixed << setprecision(3) << right << setw(20) << average << "\n\n";
  outfile << "Values Stored in Array" "\n";

  for(int n = 0; n < index; n++)
    {
      outfile << bubblesort(positive_num[n], index);
    }
}

void bubblesort(int list[ ],int count)
// Sort an array of integers into descending order.                                                                                                                                                                                                                                                                          
// Parameters:                                                                                                                                                                                                                                                                                                               
//     list: array of integers to be sorted                                                                                                                                                                                                                                                                                  
//     count: (integer) number of values in the array                                                                                                                                                                                                                                                                        
// Value passed back: sorted list                                                                                                                                                                                                                                                                                            
{
  int temp;   //place holder when values are interchanged                                                                                                                                                                                                                                                                    
  for (int i=0; i < count-1; i++)
    for (int j=0; j < count-(i+1); j++)
      if (list[j] < list[j+1])
        {
          temp = list[j];
          list[j] = list[j+1];
          list[j+1] = temp;
        }
}
the numbers i used in my file were...

51 144 5 16 8 0 -2 14 6 -4 173 263 11 9345 -135 777
main.cpp(60): error C2664: 'bubblesort' : cannot convert parameter 1 from 'int' to 'int []'

outfile << bubblesort(positive_num[n], index);

You need to pass an array to the bubblesort function, but you pass only an int.

i thought thats what i did by putting positive_num[n] in there.. how would i go about inserting an array?
Just pass the name of the array, positive_num, rather than positive_num[n] which is a single element within the array.
Thanks i was able to get it to work... how would i format these numbers to print as for example 1 2 3 4
5 6 7 8
9..... ???
If I understand correctly, you want to print four numbers per line? You could use an integer count. Add 1 to it each time a number is printed. When it reaches 4, output a newline and reset the count to zero. (could also use the modulo operator % in similar code).
i figured it out thanks..
Last edited on
can somebody help me build a function please? I want to take the code for my numbers out of the get_data function and make a separate function for them.

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
                                                                                                                                                                                                   
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;

void get_data(int);
void bubblesort(int[], int);

int main ()
{
  int list, i = 0;

  get_data(list);

  return 0;
}


void get_data(int list)
{
  string filename, filename2;
  ifstream infile;
  ofstream outfile;
  int index = 0, count = 0, positive_num[30];
  double average = 0.0, sum = 0.0;

  cout << "Please enter the name of the input file." << endl; //prompt user for input file                                                                                                                                                    
  cin >> filename;
  infile.open(filename.c_str());   //opens input file                                                                                                                                                                                         

  cout << "Please enter the name of the output file." << endl; //prompt user for output file                                                                                                                                                  
  cin >> filename2;
  outfile.open(filename2.c_str()); //opens output file                                                                                                                                                                                        

  while(infile >> list)
    {
      if(list < 0){
        count++;
        sum = sum + list;
      }
      else
        {
        positive_num[index] = list;
        index++;
        }
    }
  average = sum / count;

  infile >> list;
  outfile << "Kevin Hunt   Section #1010   Assign #4" "\n\n";
  outfile << "Negative Values" "\n";
  outfile << "Count: " << right << setw(29) <<  count << "\n";
  outfile << "Sum: " << fixed << setprecision(3) << right << setw(31) << sum << "\n";
  outfile << "Average: " << fixed << setprecision(3) << right << setw(27) << average << "\n\n";

  if(index > 0){
    outfile << "Values Stored in Array" "\n";
    bubblesort(positive_num, index);
    int reset = 1;
    double count2 = 0.0, average2 = 0.0, variance = 0.0;
    for(int n = 0; n < index; n++)
      {
        count2 = count2 + positive_num[n];

        if(reset < 4){
          outfile << right << setw(8) <<  positive_num[n];
          reset++;
        }
        else {
          reset = 0;
          outfile << right << setw(8) << positive_num[n] <<  "\n";
          reset++;
        }
      }
    average2 = count2 / index;
    for(int i = 0; i<index; i++)
      {
        variance = variance + pow((positive_num[i] - average2),2)/index;
      }


    outfile << "\n\n"  "Statistics for Data in Array" "\n";
    outfile << "Count: " << right << setw(29) << index << "\n";
    outfile << "Average: " << right << setw(27) << average2 << "\n";
    outfile << "Variance: " << right << setw(26) << variance << "\n";
    outfile << "Standard Deviation: " << right << setw(16) << sqrt(variance) << "\n\n";

    outfile << "Primes Stored in Array" "\n";
    for(int i = 0; i<index; i++)
      {
        int count = 0;
        for(int d = 2; d <= positive_num[i]; d++)
          {
            if((positive_num[i] % d) == 0)
              {
                count++;
              }
          }
            if(count == 1)
              {
                outfile << right << setw(8) << positive_num[i];
              }
      }

    outfile << "\n\n" "Nonprime" "  " "Factor" "\n"  "  Number" "   " "Count";
    int npcount = 0;
    for(int i = 0; i<index; i++)
      {
        int count = 0;
        for(int d = 2; d <= positive_num[i]; d++)
          {
            if((positive_num[i] % d) == 0)
              {
                count++;
              }
          }
        if(count > 2)
          {
            npcount++;
            outfile << "\n" << right << setw(8) <<  positive_num[i] << right << setw(8) << count + 1;
          }
      }
    outfile << "\n" "Nonprime number (>0) count: " << npcount;
  }
  else{
    outfile << "No values greater than or equal to 0 in file." << endl;
  }
 }

void bubblesort(int list[ ],int count)
// Sort an array of integers into descending order.                                                                                                                                                                                           
// Parameters:                                                                                                                                                                                                                                
//     list: array of integers to be sorted                                                                                                                                                                                                   
//     count: (integer) number of values in the array                                                                                                                                                                                         
// Value passed back: sorted list                                                                                                                                                                                                             
{
  int temp;   //place holder when values are interchanged                                                                                                                                                                                     
  for (int i=0; i < count-1; i++)
    for (int j=0; j < count-(i+1); j++)
      if (list[j] > list[j+1])
        {
          temp = list[j];
          list[j] = list[j+1];
          list[j+1] = temp;
        }
}
I suggest you think in terms of describing in words what the various parts of the program need to be. Don't worry about the code. For example there are various activities which seem separate and unrelated, those might be candidates for separate functions. (though some might be merged together).

examples:
• get names of files as input from user
• read from input file into an array
carry out various actions using the array contents:
• negative numbers
• positive numbers
• prime numbers
• statistics

Those are just ideas, don't take them as fixed in any way. Some may need further subdivision, for example testing whether a number is prime.

Then the main() function would consist of calling some of these functions directly, so that reading through main() gives one a good idea of what the program is trying to achieve, while the details are contained in their own functions.
Topic archived. No new replies allowed.