Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- #include "dinalloc.h"
- using namespace std;
- // returns -1 if the element 'str' doesn't exist in the array
- // otherwise it returns the position of the element
- int exist(hi *v, int n, str s) {
- int i = 0;
- while ((strcmp(v[i].name, s) != 0) && (i < n))
- i++;
- if (i == n)
- return -1;
- else
- return i;
- }
- void loadHistogram(hi *v, int &n) {
- int newcap = 0; // number of the item without duplicates
- for (int i = 0; i < n; i++) {
- str s;
- cout << "Enter a name: ";
- cin >> s;
- if (exist(v, n, s) == -1) { // if not exist it is a new item
- strcpy(v[newcap].name, s);
- v[newcap].cnt = 1;
- newcap++;
- } else // otherwise it exists, so increase the counter
- v[exist(v, n, s)].cnt++;
- }
- n = newcap; // set the array cap with the real number of items (without duplicates)
- }
- void printHistogram(hi *v, int n) {
- for (int i = 0; i < n; i++) {
- cout << v[i].name << "\t\t" << v[i].cnt << " ";
- for (int j = 0; j < v[i].cnt; j++)
- cout << "o";
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement