Hi,
I am a C++ beginner and was assigned the following project:
Write a program that reads in a list of positive integers from the keyboard into an array. You may assume that there are fewer than 50 entries in the array and may use a sentinel value such as -1 to indicate the end of input. Your program determines how many entries there are. The output is to be a two-column list. The first column is a list of the distinct array elements; the second column is the count of the number of occurrences of each element. For example, for the input:
12 3 12 4 1 1 12 1 1 2 3 4 2 3 12 -1
the output should be
N Count
12 4
3 3
4 2
1 4
2 2
My code so far is the following:
#include <iostream>
using namespace std;
const int columns = 2;
const int max_values = 50;
int fill_array (int list[], int table[][columns]);
void increasing (int list[]);
int count (int list[], int table[][columns]);
void show (int table[][columns]);
int main()
{
int list[max_values];
int table[max_values][columns];
fill_array (list, table);
increasing (list);
count (list, table);
show (table);
return 0;
}
int fill_array (int list[max_values], int table[max_values][columns])
{
int number, size = 0;
cout << "Enter a list of positive integers.\n"
<< "(Use -1 to end list.)\n";
for (int i = 0; i < max_values; i++)
{
cin >> number;
if (number >= 0)
{
list[i] = number;
size++;
}
else
break;
}
return size;
}
void increasing (int size, int list[])
{
int temporary;
for (int i = 0; i < size-1; i++)
for (int j = 0; j < size; j++)
if (list[i] > list[j])
{
temporary = list[i];
list[i] = list[j];
list[j] = temporary;
}
return;
}
int count (int size, int list[], int table[][columns])
{
int i = 0, j = 0, size2 = 0, count = 0;
for (int i = 0; i < size; i++)
{
j = list[i];
while (list[i] == j)
{
count++;
i++;
}
}
table[size2][0] = j;
table[size2][1] = count;
size2++;
return size2;
}
void show (int size2, int table[][columns])
{
for (int i = 0; i < size2; i++)
cout << table[i][0] << "\t" << table[i][1] << endl;
return;
}
When I compile I get
/tmp/cciDc3BA.o: In function `main':
example.cpp:(.text+0x2a6): undefined reference to `increasing(int*)'
example.cpp:(.text+0x2be): undefined reference to `count(int*, int (*) [2])'
example.cpp:(.text+0x2cc): undefined reference to `show(int (*) [2])'
collect2: ld returned 1 exit status
Help please :)