Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Im trying to create a program that does various math operations, and i wanted to start with calculating prime numbers within a given range. However, when i try to execute the code, it just returns exit status -1. What is wrong with the program and how do i fix it?
#include <iostream>
#include <vector>
using namespace std;
void getPrimes(int min, int max) {
int range = max - min;
std::vector< int > possible_values;
for (int q = 0; q < range; q++) {
possible_values.push_back(min + q);
}
for (int i = 0; i < range; i++) {
int num_of_factors = 0;
int num = possible_values.at(i);
for (int c = 0; c < num; c++) {
if (num % c == 0) {
num_of_factors++;
}
}
if (num_of_factors == 0) {
std::cout << num << endl;
}
}
}
int main() {
int min, max;
std::cout << "min: ";
std::cin >> min;
std::cout << "max: ";
std::cin >> max;
getPrimes(min, max);
}
this loop should start from 2 because :
c%0 is undefined behavior
every number %1 is 0 so you can see why num_of_factors is never 0
for (int c = 2; c < num; c++) {
if (num % c == 0) {
num_of_factors++;
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 26 days ago.
Improve this question
Sorry for bad English .
I was trying to write a program that gets a number and see if the digits of an entered number are repeated or not . I did try to if(analyse[0]==analyse[1]==analyse[2]==...) but since I don't know exactly how many elements will array have, it didn't work
#include<iostream>
int main(){
int number,number_help;
const int count{10};
std::cin>>number;
number_help = number ;
int digitcount{0};
while(number_help>0){
number_help/=10;
digitcount+=1;
}
int analyse[count]{};
for(size_t i {0}; i<digitcount ; i++){
analyse[i] = number%10;
number/=10;
}
//I don't know what to code here
return 0;
}
Change your approach: count how many there are of each digit instead of comparing them to each other.
This is much simpler.
Example:
#include<iostream>
int main(){
int number;
std::cin >> number;
const int count{10};
int frequency[count]{};
do {
frequency[number % 10] += 1;
number /= 10;
} while (number != 0);
for (int i{0}; i < count; i++) {
if (frequency[i] > 1) {
std::cout << i << " was repeated " << frequency[i] << " times.\n";
}
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
#include<iostream>
using namespace std;
int getSum(int *arr, int size) {
if(size == 0) {
return 0;
}
if(size == 1 )
{
return arr[0];
}
int remainingPart = getSum(arr+1, size-1);
int sum = arr[0] + remainingPart;
return sum;
}
int main() {
int arr[100], n;
cin >> n;
for(int i = 0; i < n; i++){
cin >> arr[n];
}
int sum = getSum(arr, n);
cout << "Sum is " << sum << endl;
return 0;
}
OUTPUT
3
1
2
3
Sum is -1827678256
When i run the code with custom input it gives the correct output. But when i input data from the user it gives an incorrect answer. Why so?
cin >> arr[n];
should be
cin >> arr[i];
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
When I pass only Sum as Parameter in My coin Exchange Problem to find no of ways to find the amount Sum..
Example
if the coins are {2,3,5} and the desired sum is 9 i get correct output as 8.
This code :
#include <bits/stdc++.h>
using namespace std;
int n, sum, a[105];
int fun(int sum) {
int S = 0;
if(sum == 0) return 1;
if(sum < 0) return 0;
for(int i=1;i<=n;i++) {
S += fun(sum - a[i]);
}
return S;
}
int main()
{
cin >> n;
cin >> sum;
for(int i=1;i<=n;i++ ) {
cin >> a[i];
}
cout << fun(sum);
return 0;
}
But when I also give Current Index as a parameter it gives me output as 3 which is wrong.
This code :
#include <bits/stdc++.h>
using namespace std;
int n, sum, a[105];
int fun(int sum, int idx) {
int S = 0;
if(sum == 0) return 1;
if(idx > n) return 0;
if(sum < 0) return 0;
for(int i=idx;i<=n;i++) {
S += fun(sum - a[i], i);
}
return S;
}
int main()
{
cin >> n;
cin >> sum;
for(int i=1;i<=n;i++ ) {
cin >> a[i];
}
cout << fun(sum,1);
return 0;
}
But WHY??
Is my parameter passing in this case is wrong?
You don't have the same result between your 2 solutions because in the first, at each "function call", you will iterate from 1 to N and in your second solution, you will iterate from Index to N. So the number of loop will be different.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
so I have this code that with a function is supposed to take all the numbers in a 2D array and print them to the second power but my code keeps throwing segmentation fault and i don't know why
#include <bits/stdc++.h>
using namespace std;
void er(int arr[][100000000], int, int);
int main()
{
int n, m;
cin >> n >> m;
int arr[n][100000000];
er(arr, n, m);
return 0;
}
void er(int arr[][100000000], int n, int m)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
arr[i][j] *= arr[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << arr[i][j];
}
}
}
Using
int arr[n][100000000];
is problematic on two accounts.
VLAs are not standard C++. It is supported by some compilers as an extension.
The size 100000000 is too large for a variable on the stack. Changing that to 100 and making sure that m is less than or equal to 100 will most likely work as long as your compiler supports VLAs.
A better alternative would be to use std::vector.
int n, m;
cin >> n >> m;
std::vector<std::vector<int>> arr(n, std::vector<int>(m));
Of course, that will require you to change the function er accordingly.
In addition, please don't use
#include <bits/stdc++.h>
See Why should I not #include <bits/stdc++.h>? for further details.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to solve algorithmic problems on the UVa online judge but I am stuck on the 3n+1 problem. I see the right output every time, but the judge says it is a wrong answer. Why?
Also, how can I optimize this code so that it does not take a long time for 1000000?
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main(){
int a;
int b;
int cyclelength(int i);
while (scanf("%d %d\n",&a,&b)==2){
int max = 0;
if (b < a){
for (int i = b; i < a; i++){
if (cyclelength(i) > max)
max = cyclelength(i);
}
}
else
{
for (int i = a; i < b; i++){
if (cyclelength(i) > max)
max = cyclelength(i);
}
}
cout << a << " " << b << " " << max << endl;
}
}
int cyclelength(int i){
if(i==1)
return 1;
if(!(i%2))
return cyclelength(i/2)+1;
else
return cyclelength(3*i+1)+1;
}
Your submission is incorrect because you aren't iterating over the range from a to b, inclusive.
The problem statement reads:
For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j.
Your code loops over
for (int i = b; i < a; i++)
and
for (int i = a; i < b; i++)
thus omitting a in the former case and b in the latter.
You should change the loops to
for (int i = b; i <= a; i++)
and
for (int i = a; i <= b; i++)
You can speed up your code somewhat by memoizing the results of cyclelength. This lets you look up previously computed values instead of spending time on computing them again.
Here is a memoized version of cyclelength:
#include <map>
using namespace std;
map<long long, int> memo;
int cyclelength(long long i) {
if (i == 1) {
return 1;
}
if (memo.count(i) == 1) {
return memo[i];
}
if (i%2 == 0) {
return memo[i] = 1 + cyclelength(i/2);
} else {
return memo[i] = 1 + cyclelength(3*i + 1);
}
}
An unordered_map would be faster, but the UVA judge returned a compilation error for #include <unordered_map>.
Note that my version of cyclelength takes a long long argument. That lets me calculate cyclelength(999999), which eventually causes an integer overflow due to repeated 3n+1 operations if you limit yourself to 32-bit integer values. The problem statement promises that "no operation overflows a 32-bit integer" and you can get your submission accepted if you only use int values, but you have to use a larger integer type if you want to compute cyclelength for values in the range [1, 1000000).