I am making a program in which
the program takes 3 numbers as input: "l", "r" and "a".
I get all the values of "x" between l and r, (l and r inclusive).
example, l = 1, r = 3, x values are 1, 2, 3. so now I have a function, f(n) = ((x/a) + (x % a)),(note: [x/a] is rounded down to an integer). so I have implemented this in c++ and my code is below.
#include<iostream>
using namespace std;
int main()
{
int l;
int r;
int a;
cin>>l>>r>>a;
int nums[(r-l)+2];
int answers[(r-l)+2];
for (int i = 1; i < (r-l)+2; i++)
{
nums[i] = i;
}
for (int i = 1; i < sizeof(nums)/sizeof(nums[0])-1; i++)
{
answers[i] = ((nums[i]/a) + (nums[i] % a));
}
int j = 0;
j = answers[0];
for (int i = 0; i < sizeof(answers); i++)
{
if (j < answers[i])
{
j = answers[i];
}
}
cout<<j;
}
but whenever I run this code, I get huge random numbers like 230984084 and all.So please point out what's wrong with my Code. Thanks in advance.
Request from Levi to post my comment as answer:
The point is that one of the successes of C++ is that it started from C, but it moved on quite a bit and the code in your question is still mostly C-code. Here is an example how it could be handled in C++ with more knowledge needed but less chance for errors:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <numeric>
int main()
{
std::cout << "Give 'left', 'right' and 'process' values: \n";
int l, r, a;
std::cin >> l >> r >> a;
std::vector<int> nums((r-l)+2);
std::vector<int> answers;
// fill container with ascending numbers
std::iota(nums.begin(), nums.end(), 1);
// transform as needed
std::transform(nums.begin(), nums.end(), std::back_inserter(answers), [a](int i) { return i/a + i%a; });
// find maximum element in container (returns iterator to element)
auto maxv = std::max_element(answers.begin(), answers.end());
std::cout << *maxv;
}
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<double> numbers(input_number);
cout << "Enter numbers from 0 to 50: " << endl;
for (int i = 0; i < input_number; ++i) {
cin >> numbers[i];
}
unordered_map<int, int> freq;
for (int i = 0; i < numbers.size(); i++) {
freq[numbers[i]]++;
}
for (int i = 0; i < numbers.size(); i++) {
cout << freq[numbers[i]];
}
return 0;
}
When the use inputs numbers, for example 1,1,1,2 the output should be "1" because it is the most frequent number but the output here became "3,3,3,1" How to solve this problem?
You are most of the way there. Your frequencies are all stored, and you just need to search through the unordered_map now to find the item that has the largest value. Since you're already using <algorithm> you can leverage std::max_element to help:
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>
int main()
{
std::vector<int> numbers = { 1, 2, 3, 2, 3, 2, 2 };
std::unordered_map<int, int> freq;
for (int n : numbers) ++freq[n];
if (!freq.empty())
{
auto it = std::max_element(freq.begin(), freq.end(),
[](const auto& a, const auto& b) { return a.second < b.second; });
std::cout << "Most frequent is " << it->first << "\n";
}
}
Output:
Most frequent is 2
Here, a custom comparison function is supplied that tests only the frequency part of the element (otherwise the default behavior will compare the full key/value pairs which will result in finding the largest number).
Note that because the map is unordered, there won't be a predictable outcome for tie-breakers (where more than one number is the most frequent). If you need to handle that in a more predictable way, you'll need to adjust the comparison function or possibly just loop over the container yourself if it requires additional work.
One option for tie-breaking is to choose the lowest number. That could be achieved by modifying the comparison to:
return std::make_pair(a.second, b.first) < std::make_pair(b.second, a.first);
The problem is that you just output all the values in map. In a naive implementation you have to iterate through map and register the maximum value and it's frequency:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
int main()
{
using std::cin, std::cout, std::endl;
using std::vector, std::unordered_map;
int input_number = 0, max_freq = 0, max_val = 0;
cout << "How many numbers you want to input: " << endl;
cin >> input_number;
// Making input double may creates questions
vector<int> numbers(input_number);
cout << "Enter numbers from 0 to 50: " << endl;
for (int i = 0; i < input_number; ++i) {
cin >> numbers[i];
}
unordered_map<int, int> freq;
for (int i = 0; i < numbers.size(); i++) {
freq[numbers[i]]++;
}
// iterating over the map and finding max value
for (auto val : freq) {
if( val.second > max_freq) {
max_val = val.first;
max_freq = val.second;
}
}
cout << max_val;
return 0;
}
Standard maps do store values as pairs of key and value (std::pair). This can be done in easier way: you can do that right while inputting the numbers.
for (int i = 0; i < numbers.size(); i++) {
int val = numbers[i];
int curfreq = ++freq[val];
if (curfreq > max_freq) {
max_val = val;
max_freq = curfreq;
}
}
cout << max_val;
Your array freq has to proper values in it. But your logic in the print out is wrong. You are printing the numbers twice. I guess you want to print all the numbers from 0 to 50.
cout << freq[i]
Then you see which entries have values or not. Then add some logic (which doesn't exist in your code) to pick the proper value. Like the biggest count..
I'm trying to pass random integers (between 0 and 11) to the Numbers[] array, but i have to make sure that all 10 of its elements are different. I've tried to pass the numbers first in the array, and then check if there are any numbers that are equal but its not working this way. Here's my code:
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
int Numbers[10];
srand( time(NULL) );
for (int i = 0; i < 10; i++ )
{
Numbers[i] = rand() % 12; // First, the integers are passed
to the array (They must be between 0 and 11)
cout << Numbers[i] << endl; // and printed to the screen
}
cout << endl << endl;
for (int u = 0; u < 10; u++)
{
if(Numbers[u] == Numbers[u - 1]) // If there are two equal
numbers
{
switch (Numbers[u]) // One of them is incremented (But that
causes problems as well because it can lead to another pair of equals)
{
case 11: // In case one of them is 11
Numbers[u]--;
break;
default:
Numbers[u]++;
break;
}
}
cout << Numbers[u] << endl;
}
return 0;
}
Halp!
Just use std::vector, std::iota and std::shuffle:
std::vector<int> v( 12 );
std::iota( v.begin(), v.end(), 0 ); // initialize with values 0..11
std::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()}); // make em random
v.resize( 10 ); // remove extra elements
and you do not need to validate that all elements are unique
What I understood from your question that you are trying to read random numbers till all 10 numbers are different. Have a look on code below:
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <bitset>
using namespace std;
int main()
{
int Numbers[10] ;
srand(time(NULL));
//Keep flag with all the bits '0' initially
bitset<12> flags;
flags.reset();
// keep taking input till all bits are not '1'
int i = 0;
do
{
int in_num = rand() % 12;
// check if the bit at position "in_num" is 0
if (!flags[in_num])
{
Numbers[i++] = in_num;
flags.set(in_num);// set the bit 1
}
} while (i < 10);
for (int u = 0; u < 10; u++)
{
cout << endl << Numbers[u];
}
return 0;
}
I need to compare two arrays and output another array that shows common elements.
The output I'm expecting with my code is: 0000056789.
Help will be appreciated.
#include <iostream>
using namespace std;
const int CE = 10;
const int TOP = CE-1;
int iArr1[CE]={0,1,2,3,4,5,6,7,8,9};
int iArr2[CE]={5,6,7,8,9,10,11,12,13,14};
int iArr3[CE]={0,0,0,0,0,0,0,0,0,0};
void main()
{
int i;
int j;
int iCarr3 = 0;
for(i=0; i<=TOP; i++)
{
for (j=0; j<=TOP; j++)
{
if (iArr1[i]==iArr2[j])
{
iCarr3++;
iArr3[iCarr3]=iArr2[j];
}
}
}
cout << iCarr3 << endl;
cout << iArr3;
getchar();
}
you are printing the address of your array
to print the elements of an array
for (int i = 0; i < size; i++) // keep track of the size some how
cout<<iArr3[i]<<" ";
P.S: consider sorting the arrays first, and ckecking if iArr1[i] > iArr2[j]that way you won't need to scan all the elements on eavh pass
C++ has a set_intersection algorithm in the Standard Library:
#include <iostream>
#include <algorithm>
int main()
{
const int CE = 10;
int iArr1[CE] = {0,1,2,3,4,5,6,7,8,9};
int iArr2[CE] = {5,6,7,8,9,10,11,12,13,14};
int iArr3[CE] = {0,0,0,0,0,0,0,0,0,0};
std::set_intersection(std::begin(iArr1), std::end(iArr1),
std::begin(iArr2), std::end(iArr2),
std::begin(iArr3));
std::copy(std::begin(iArr3), std::end(iArr3), std::ostream_iterator<int>(std::cout, " "));
}
Output
5 6 7 8 9 0 0 0 0 0
Note
If your arrays aren't already sorted, you could put the data into a std::set first, since std::set_intersection() requires the inputs to be sorted.
Ok I hope this is not homework. The way you have it, the output will be 5678900000. For the output to be as you want change your code to be as such:
for(i=0; i<=TOP; i++)
{
for (j=0; j<=TOP; j++)
{
if (iArr1[i]==iArr2[j])
{
iArr3[iCarr3]=iArr2[j];
}
}
iCarr3++;
}
Then for the output do this:
for(int k = 0; k <= iCarr3; k++)
std::cout << iArr3[iCarr3] << " ";
As your array are sorted, use std::set_intersection. Otherwise you just have to std::sort them before.
But never forget to use the STD library, the code is a lot more compact and readable... And most of the time less buggy and faster that what you'll come with.
http://ideone.com/c3xE3m
#include <algorithm>
#include <iostream>
#include <iterator>
const size_t CE = 10;
int iArr1[CE]={0,1,2,3,4,5,6,7,8,9};
int iArr2[CE]={5,6,7,8,9,10,11,12,13,14};
int iArr3[CE]={0,0,0,0,0,0,0,0,0,0};
int main(int argc, char const *argv[])
{
auto end_elemnt =
std::set_intersection(iArr1, iArr1 + CE,
iArr2, iArr2 + CE,
iArr3);
std::copy(iArr3, end_elemnt, std::ostream_iterator<int>(std::cout, ", "));
return 0;
}
Here is the output:
$ ./a.exe
5, 6, 7, 8, 9,
I need to find the biggest sum and it containing numbers and whether the first or second number out of two was bigger. How to find it?
Let's say that n=10, the two put numbers are 6 and 2, followings: 7 and 1,
5 and 6, 1 and 8, 4 and 3. Then the answer should be that the biggest sum is 11, it containing numbers are 5 and 6, and the bigger numb was the second one.
I have a code:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int p, a, i;
int n;
int sum;
ofstream fr("Rezults.txt");
ifstream fd("Data.txt");
fd>>n;
cout<<n<<endl;
for (i=1; i<=n/2; i++)
{
fd>>p>>a;
sum=p+a;
for (int j=sum; j<=n/2; j++);
{
cout<<sum<<endl;
}
}
fd.close();
fr<<sum;
fr.close();
return 0;
}
I think your code should be like this :
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int p, a, i;
int n;
int sum;
ofstream fr("Rezults.txt");
ifstream fd("Data.txt");
fd>>n;
cout<<n<<endl;
fd>>p>>a;
int biggestSum=p+a;
int first = p;
int second = a;
for (i=2; i<=n/2; i++)
{
fd>>p>>a;
sum=p+a;
if(sum > biggestSum)
{
biggestSum = sum;
first = p;
second = a;
}
}
cout <<"biggest sum is "<<biggestSum<<"\n";
cout <<"The first number is "<<first<<"\n";
cout<<"The second number is "<<second<<"\n";
fd.close();
fr<<sum;
fr.close();
return 0;
}
updated : you should be careful to the index i of the for loop it should start by 2 since you read the first two numbers before the for loop.