Below is my home work problem. I don't want the answer, I am just looking for an example of how I can count the occurance of each number in the array numbers and assign that value to the count array.

Thanks for your help!!!!!!


*---------------------------------------…
* Sample Output:

*** start of 27610_Arrays03.cpp program ***

Number Count
1 1
2 2
3 5
4 2
5 3

*** end of 27610_Arrays03.cpp program ***

Input:
No data comes from the keyboard; all data, including the data
for the array, are contained within the program.

An integer array of 13 numbers: 3, 4, 5, 3, 2, 1, 3, 4, 5, 3, 2, 3, 5
An integer array of 5 counts intialized to all 0.
Compile-time arrays with initialization list of numbers.
NOTE: In main() function

Processing:
TODO #1: complete the coding of the numbers array

TODO #2: complete the coding to call the countValues() function
The values in the numbers array are counted
and the correct cells of the count array are increment.
Output:
TODO #3: complete the coding to call the display() function,
Display as shown in sample output.
Use iomanipulators to format output

TODO #4: complete the coding of the display() function
TODO #5: complete the coding of the countValues() function

*/

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

int main(void)
{
// constants
const int VALUES_SIZE = 13;
const int COUNT_SIZE = 5;

// function prototype(s)
// return by value is used
void countValues(const int[], const int, int[]);
void display(const int[], const int); //(const int [], const int);

// local data

// data declaration(s) & initialization(s)
// array data: use data from sample output above
// TODO #1: declare & initialize array of ten integers
int numbers[VALUES_SIZE] = {3, 4, 5, 3, 2, 1, 3, 4, 5, 3, 2, 3, 5};
int counts[COUNT_SIZE] = {0};

// start the program
cout << "*** start of 276Arrays_03.cpp program ***" << endl;
cout << endl;

// Compute count of values
// TODO #2 : call countValues() function
// passing numbers array, array size, counts array
countValues(numbers, VALUES_SIZE, counts);

// display the required output
// TODO #3: call display function,
// passing count array & size
display(counts, COUNT_SIZE); //note you used numbers twice needs to be changed to count

// terminate the program
cout << endl;
cout << endl;
cout << "*** end of 276Arrays_03.cpp program ***" << endl << endl;

cin.get();
return 0;
} // end main()

//------------------------------------…
// Function Name: display(const int[], const int):void
// Purpose: to display count of values
// Values received: array of counts, array size
// Values returned: <none>
// Notes:
// display column headings using manipulators
// in a 'for' loop, print the number and count
//------------------------------------…
// TODO #4: code the display function
void display(const int count[], const int COUNT_SIZE)
{

// print column headings
cout << left << setw(10) << "Number";
cout << left << setw(10) << "Count";
cout << endl;
// print detail lines (format on one line, display on next)
// use a 'for' loop

for(int i = 0; i < 5; i++)
// format & print number 
{ cout << right;
cout << setw(3);
cout << i+1;
// format & print count of number
cout << right;
cout << setw(10);
cout << count[i];
cout << endl;
}
}

//------------------------------------…
// Function Name: countValues(const int numbers[],
// const int size,
// int counts[])
// Purpose: to display count of values
// Values received: array of numbers, array size, array of counts
// Values returned: <none>
// Notes:
// display column headings using manipulators
// in a 'for' loop, print the number and count
//------------------------------------…
// TODO #5: code the countValues function
void countValues(const int numbers[], const int VALUES_SIZE, int counts[])
{
for (int i = 0; i < VALUES_SIZE; i++)
{

}
// determine value and increment count of that value
// use a 'for' loop
}

Recommended Answers

All 2 Replies

>>I am just looking for an example of how I can count the occurance of each number in the array numbers and assign that value to the count array.

what ideas do you have? Does not have to be code, just a general idea?

Since I can't read your code because of the lack of formatting I'll just give you a generic idea using

An integer array of 13 numbers: 3, 4, 5, 3, 2, 1, 3, 4, 5, 3, 2, 3, 5
An integer array of 5 counts intialized to all 0.

counts[numbers[i]]++; // increment the count for number[i]

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.