Suppose you have a single linked list of integers, the numbers could be repeated
ID: 3759501 • Letter: S
Question
Suppose you have a single linked list of integers, the numbers could be repeated consecutively. Write a function that will count how many times a number is repeated, the function should printout which number was repeated the most and also printout how many times it was repeated. For example, if the single linked list has the following numbers: 2446577788112222 The function should print: The number that was repeated the most was: 2 It was repeated 4 times Make sure to have the function very general.Explanation / Answer
include<iostream>
using namespace std;
void main()
{
int a[5];
int b[5];
int p;
int i;
for (p=0; p<5; p++){
cout<<"Enter the numbers ";
cin>>a[p];
}
int flag=1;
int temp;
int j;
for(i = 0; (i <5) && flag; i++)
{
flag = 0;
for (j=0; j < 4; j++)
{
if (a[j+1] < a[j])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
flag = 1;
}
}
}
b[0]=a[0];
int d,m=1;
for(d=1;d<5;d++)
{
if(a[d]!=a[d-1])
{
b[m]=a[d];
m++;
}
}
for(int i = 0; i < 5; ++i)
{
std::cout << a[i] << "was repeated " << std::count(a, a + 5, a[i]) << " times. " << std::endl;
}
}
include<iostream>
using namespace std;
void main()
{
int a[5];
int b[5];
int p;
int i;
for (p=0; p<5; p++){
cout<<"Enter the numbers ";
cin>>a[p];
}
int flag=1;
int temp;
int j;
for(i = 0; (i <5) && flag; i++)
{
flag = 0;
for (j=0; j < 4; j++)
{
if (a[j+1] < a[j])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
flag = 1;
}
}
}
b[0]=a[0];
int d,m=1;
for(d=1;d<5;d++)
{
if(a[d]!=a[d-1])
{
b[m]=a[d];
m++;
}
}
for(int i = 0; i < 5; ++i)
{
std::cout << a[i] << "was repeated " << std::count(a, a + 5, a[i]) << " times. " << std::endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.