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
}

Dani AI

Generated

A few clarifications and a short plan that builds on ’s hint for and the follow-up from .

Keep it simple and safe: pick how array values map to count indices, validate that mapping, then scan the input and increment the appropriate counter. Steps to follow:

  1. Make sure the counts storage is zeroed before starting (use an initializer or fill it).
  2. Decide the index mapping. If your values are 1..N, map a value v to index v-1; if they’re 0..N-1 use v directly. Always check v is inside the expected range before incrementing.
  3. Loop over the numbers array, compute the index for each value, guard against out-of-range values, and increment the counter.
  4. After counting, print results with a header and one line per value (use setw to align columns if you want nicer output).

If the value set isn’t a small contiguous range (or you don’t know the range in advance), use an associative counter instead of a fixed-size array. That avoids bounds checks and is more general:

#include <unordered_map>

// counts will grow to include any integer you see
std::unordered_map<int,int> counts;
for (int v : numbers) ++counts[v];

Troubleshooting tips: test with boundary values (minimum and maximum expected), add an assertion or a debug print when an out-of-range value appears, and prefer named constants for sizes to avoid magic numbers. For modern C++ use std::array or std::vector and range-based for loops to make the code clearer and safer.

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.