1.11M Members

Problem Printing Function

 
0
 

I'm trying to write a program that asks the users for a set of numbers which the program then sorts and counts the frequency of. I struggling with understanding arrays. I have one array in particluar called frequencyArray that I'm trying to print but it keeps returning the value 0.

            // Includes
            #include<iostream>
            #include<iomanip>
            #include<cmath>
            #include<string>
                using namespace std;

            // Function Prototypes
            void showIntro(int, int, int, string);
            int fillArray(int array[]);
            void showArray(int array[], int n);
            void bubbleSort(int array[], int n);
            int frequencyArray(int dataArray[], int valueArray[], int countArray[], int n);
            void swap(int array[], int i, int j);

            // start of main
            int main(void) {

                // call a function to display splash screen
                showIntro(3, 30, 2012, "This program will will create a frequency table for a list of values.");
                cout << endl; // because month is 3, using the switch it will recognize 3 as March

                // declare variables
                int dataArray[50]; // create an array of doubles
                int arraySize = 0;
                int valueArray[50];
                int countArray[50];
                int frequencyValue = 0;
                int n = 0;

                // Fill the array
                arraySize = fillArray(dataArray);

                // table header
                cout << endl;
                cout << "Value" << "   Frequency" << endl;

                // print the array
                showArray(dataArray, arraySize);

                // sort the array
                bubbleSort(dataArray, arraySize);

                // print the array
                showArray(dataArray, arraySize);

                //print the frequency array
                frequencyValue = frequencyArray(dataArray, valueArray, countArray, n);

                system("pause");
                return 0;
            }

            // function to print splash screen
            void showIntro(int m, int d, int y, string descript) { // declare variables

                // string for month
                string month = "";

                // cout << "March 30, 2012" << endl;
                // use parameters to set date

                //switch to set the month string
                switch(m) {
                case 1:
                    month = "January";
                    break;
                case 2:
                    month = "February";
                    break;
                case 3:
                    month = "March";
                    break;
                case 4:
                    month = "April";
                    break;
                case 5:
                    month = "May";
                    break;
                case 6:
                    month = "June";
                    break;
                case 7:
                    month = "July";
                    break;
                case 8:
                    month = "August";
                    break;
                case 9:
                    month = "September";
                    break;
                case 10:
                    month = "October";
                    break;
                case 11:
                    month = "November";
                    break;
                case 12:
                    month = "December";
                    break;
                default:
                    month = "unknown";
                    break;
                }

                cout << month << " " << d << ", " << y << endl;

                cout << descript << endl;

                return;
            }
            // function to sort an array of doubles
            void bubbleSort(int array[], int n) { // use void because this is a stub function, serves as a placeholder
                // nested loop to compare each element with those that follow it
                for(int i=0; i<n-1; i++) {

                    for(int j=i+1; j<n; j++) {

                        // comparison
                        if ( array[j] < array[i]) {
                            swap(array, i, j);
                        }
                    }// end of inner loop
                } // end of outer loop

                return;
            }
            // function to swap elements at indeces i and j
            void swap(int array[], int i, int j) { 

                // switching the two values by using an intermediate temporary variable, like tower of hanoi
                int temp;
                temp = array[j];
                array[j] = array[i];
                array[i] = temp;
            }
            // function to fill array with doubles
            int fillArray(int array[]) {

                int n = -99; // Number of data point
                // Prompt for the number of data points
                cout << "How many values will you enter? ";
                cin >> n;

                // Loop to enter data
                for(int i = 0; i < n; i++) {

                    // Enter the data point
                    cout << "Enter data point " << (i+1) << ": ";
                    cin >> array[i];

                }

                return n;
            }
            // function to print array to the screen
            void showArray(int array[], int n) {

                // print a linefeed
                cout << endl;

                // Loop through the array printing the values
                for(int i = 0; i < n; i++) {
                    // Print element i
                    cout << array[i] << "\t";
                }
                // print a linefeed
                cout << endl;

            }
            // function to count frequency
            int frequencyArray(int dataArray[], int valueArray[], int countArray[], int n) {

                int frequency = 0;
                valueArray[0] = dataArray[0];
                countArray[0] = 1;

                    for(int i=1; i<=n; i++) {

                        if (dataArray[i] == valueArray[frequency]) 
                        {
                            countArray[frequency]++;
                        }
                        else 
                        {
                            countArray[frequency] = 1;
                            valueArray[frequency] = dataArray[i];
                        }
                    } // end of for loop

                return frequency;
            }
 
0
 

If you are referring to the function "frequencyArray" then it will always return zero because the variable "frequency" that is being returned has been initialized to zero and never changes throughout the function.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article