hi, could anybody help me correct my code. . .because i tried debugging it so many times i just cant find the problem so i was supposed to find for the number that occurred the most and times it occurred in my program Thanks for your help in advance ^^

``````void occur()
{
cout<<"The Number that occur the most is: ";
for(int i=0;i<9;i++)
{
for(int j=i+1;j<10;j++)
{
if(num[i]==num[j])
{
cout<<num[i]<<" ";
}
}
}
cout<<endl;
}
void times()
{
int dup=0,time=0;

for(int i=0;i<10;i++)
{
num2[i]==num[i];
num2[i]+=1;
if(num2[i]==num[i])
{
dup+=1;
}
}cout<<"The number of time that occur is: "<<dup;
}``````
3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by VernonDozier

You're trying to use 2 arrays num[] and num2[] that you never created. Also, where are the values for num[] supposed to come from?

Array Example:

``````//Create an array
int salary[10];

//Assign values to the array
//You can do this as part of a loop or whatever you want
salary[0] = 1;
salary[1] = 2;
etc....``````

If you're creating your array in main then you must pass it to your functions as the array will be local to main and will not exist in your functions.

Example:

int maximumNumber(int a[], int b[])
{
//body of function
}

ahm sorry i wasn't able to post the whole code i was just trying to get the logic of the occurrences anyway ill post my whole code

``````#include<iostream>
#include<cstdlib>
double ave=0;
using namespace std;

{
}
void multiples(int a)
{
sum1+=a;
}
void aver(int a)
{
ave=a/10;
}
void lessthan(int a)
{
l+=a;
}
void odd(int a)
{
o+=a;
}
void lowest(int arr[])
{
cout<<"The smallest element is: "<<*min_element(arr,arr+10)<<endl;
}

void occur()
{
cout<<"The Number that occur the most is: ";
for(int i=0;i<9;i++)
{
for(int j=i+1;j<10;j++)
{
if(num[i]==num[j])
{
cout<<num[i]<<" ";
}
}
}
cout<<endl;
}
void times()
{
int dup=0,time=0;

for(int i=0;i<10;i++)
{
num2[i]==num[i];
num2[i]+=1;
if(num2[i]==num[i])
{
dup+=1;
}
}cout<<"The number of time that occur is: "<<dup;
}
main()
{

cout<<"Input 10 numbers:\n";
for(i=0;i<10;i++)
{
cin>>num[i];
if(num[i]>0&&num[i]<31)
{
if(num[i]%3==0||num[i]%4==0)
{
multiples(num[i]);
}
if(num[i]<15)
{
lessthan(num[i]);
}
if(num[i]%2!=0)
{
odd(num[i]);
}
}

else
{
cout<<"Invalid Input"<<endl;
}
}
cout<<"average of all numbers: "<<ave<<endl;
cout<<"sum of all multiples of 3 and 4: "<<sum1<<endl;
cout<<"sum of all numbers less than 15: "<<l<<endl;
cout<<"sum of all odd numbers is: "<<o<<endl;
lowest(num);
occur();
times();

system("pause");
}
``````

Edited by mike_2000_17: Fixed formatting

ahm sorry i wasn't able to post the whole code i was just trying to get the logic of the occurrences anyway ill post my whole code

I don't know what you did when you posted, but it doesn't seem like you used code tags and I can't get your code to get quoted. Maybe you did a quote within a quote or something. That's what it looks like. Use code tags like you did in post 1.

As you can see, it's impossible to read. There's no formatting. Not sure if there wasn't any formatting originally or if it got stripped out in the post.

You need to go through your original array one element at a time and get a count of of how often each element occurs and store it in another array. Then get the maximum of that second array and see what index it corresponds to.

But repost with code tags and indentation. I know how I'd write it, but I can't see where your logic is the way it's formatted. You should probably use more descriptive variable names to make it easier to follow. dup, num1, num2 are a little vague.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.