Hello again,
I wrote a modular program, which
-gets 10 integers and saves it in an array,
-sorts them by value ( + displays the biggest and smallest one),
-counts the amount of identical numbers typed in-->doesn't work
The function of interest can be found at the bottom of this post !
**MAIN-FUNCTION**
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include "functions.cpp"
using namespace std;
int main(int argc, char* argv[])
{
arrayInitializer(array);
arrayDisplay(array);
sortArray(array);
displaySortArray(array);
displaySmallest_and_BiggestVal(array);
counter(array);
confirm();
fflush(stdin);
return 0;
}
|
**FUNCTIONS**
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int array[10]{0};
int count[2][10];
int arrayInitializer(int array[10])
{
int x=0;
cout<<"Please type in 10 integers:\n";
for(int i=0;i<10;i++)
{
cin>>x;
array[i]=x;
}
return *array;
}
int arrayDisplay(int array[10])
{
cout<<"\n\n"<<endl;
cout<<"This is your Array-Data:\n"<<endl;
for(int i=0;i<10;i++)
{
cout<<array[i]<<" ";
}
cout<<"\n\n"<<endl;
}
int sortArray(int array[10]){
int temp;
int i, j;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
if(array[j]>array[j+1]){ //Index changes +1, so instead of 0...10, we now have 1...11
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
return *array;
}
void displaySortArray(int array[10]){
cout<<"\n";
cout<<"This is your sorted Array:\n"<<endl;
for(int i=1;i<11;i++) //Index +1 -> See upper comment
{
cout<<array[i]<<" ";
}
}
void displaySmallest_and_BiggestVal(int array[10]){
cout<<"\n\n";
cout<<"Smallest Value: "<<array[1]<<endl;
cout<<"Biggest Value: "<<array[10]<<endl;
}
void counter(int array[10]){
for(int j=0;j<10;j++){
count[1][j]=1; //Initialise 2nd Row with 1
}
for(int i=0;i<1; i++){ //keep i constant at 0, the first row
for(int j=0;j<10;j++){
for(int r=0;r<10;r++){
if(count[i][j]==count[i][r]){ //if count[0][j]== count[0][r] (r variable from 0->9)
count[1][j]+=1; //yes: a certain number(i) occured several
//times(at least 2 times),
//so increase element cout[1][j] by 1
} else{} //else: do nothing
}
}
}
cout<<"This is your occurence-counting Array\n"<<endl;
for(int i=0;i<2;i++){
for(int j=0;j<10;j++){
cout<<count[i][j]<<"\t ";
}
cout<<endl;
}
}
void confirm(){
cout<<"In order to quit your program, please press '~' \n";
char z;
do{
cin>>z;
}
while(z!='~');
}
|
The problem I have is HOW to count this repitition,
what I tried was the following(see function "counter(int array[10])") :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
void counter(int array[10]){
for(int j=0;j<10;j++){
count[1][j]=1; //Initialise 2nd Row with 1
}
for(int i=0;i<1; i++){ //keep i constant at 0, the first row
for(int j=0;j<10;j++){
for(int r=0;r<10;r++){
if(count[i][j]==count[i][r]){ //if count[0][j]== count[0][r]
//(r variable from 0->9)
count[1][j]+=1; //yes: a certain number(i) occured several
// times(at least 2 times),
// so increase element cout[1][j] by 1
} else{} //else: do nothing
}
}
}
cout<<"This is your occurence-counting Array\n"<<endl;
for(int i=0;i<2;i++){
for(int j=0;j<10;j++){
cout<<count[i][j]<<"\t ";
}
cout<<endl;
}
}
|
Does anybody see a mistake in the design, so the general idea of how I count ?
What I intended to do with the variable "int r" is the following:
Note:
i is constant
For e.g. an element of the value 12, "count[
i][j=0]", iterate over all j.
Because this element has to stay constant, i use "r" to iterate over the elements of the 2nd row: count[
i][r]
If one of those (iterated) elements equals the constant one(here 12), increment the counter.
The loop does that for every elements[0][j], so that all typed in numbers are being checked.
Does that make sense? I seriously can't tell ^^
(one thing: Did I maybe mess up with the IMPLEMENTATION(#include "...") of the functions-code in my main-code ?)
I am thankful for every attempt to help me out here,
Have a nice evening !