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 2 years ago.
Improve this question
I was asked to fill a given vector with prime numbers, I can't use any other vectors, arrays or collections. I've come up with something like this, but it doesn't work properly and I can't figure out why.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool isPrime(int n){
if (n<=1){return false;}
for (int i = 2; i < n; i++){
if (n % i == 0){
return false;
}
}
return true;}
int fillWithPrimesVec(vector<int>& vec){
for (int i = 0; i < vec.size(); i++){
for (int j = 0; j<INT_MAX; j++){
if (isPrime(j)){
vec.push_back(j);}
else{continue;}
}
}
return vec[0];
}
int main(){
int c[15];
size_t szc = sizeof(c)/sizeof(*c);
vector<int> d(szc,0);
cout << fillWithPrimesVec(d);
return 0;
}
Could anyone please help me?
Related
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 6 years ago.
Improve this question
I need to make a function in C++, which takes a string, which contains only digits, for example:
string s = "7654321"
and converts it to a vector of integers. So vector should be like this:
vec[1] = '7'
vec[2] = '6'
etc.
I tried to use isstringstream, but that was useless in this situation, cause that string has no spaces in it.
You can use a for loop to iterate through the string, and fill the vector with each value using push_back() and -'0'
assuming vector vec;
void fillVec(const string str1, vector<char> & vec) {
for(int i = 0; i < str1.length(); i++)
vec.push_back(str1[i]) - '0';
}
Example program implementing this
// Example program
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void fillVec(const string, vector<int> &);
int main()
{
vector<int> vec;
string str1 = "1234567";
fillVec(str1, vec);
for(int i = 0; i < vec.size(); i++)
cout << vec[i] << ", ";
return 0;
}
void fillVec(const string str1, vector<int> & vec) {
for(int i = 0; i < str1.length(); i++)
vec.push_back(str1[i]-'0');
}
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 6 years ago.
Improve this question
I was debugging this, and debugger skipped the last 'if' even 'sum' was equal to 'n' and jump straight to 'else', I don't know why. Please help.
P/s: Can I use dynamic array to increase the mobility of my program?
#include <iostream>
#include <math.h>
using namespace std;
int exponent_of_10(); // set position for digits
int exponent_of_10(int a, int b){
for(int j = b; j>0;j--)
{
a *= 10;
}
return a;
}
main() //check if the number was palindromic
{
int n;
int a[6]={0,0,0,0,0,0};
int i = 0;
int temp;
int S;
cout<< "Input n (maximum of 6 digits): ";
cin>> n;
do
{
if(n<1)
{break;}
temp=n%10;
a[i]=temp;
n=(n-temp)/10;
i++;
}
while (n!=0);
int sum = 0;
for(int j=0; j<=5; j++)
{
exponent_of_10(a[j],j);
S = exponent_of_10(a[j],j);
if (S==0)
{break;}
sum +=S;
}
if(sum==n)
{
cout<< "Congratz, this is PALIDROMIC NUMBER !!";
}
else
cout<< "Sad life, this is NOT palidromic number";
return 0;
}
When the code exits the do ... while() loop, n is 0. For the test in the if to be meaningful, the code should save the original value of n somewhere and compare sum to that original value.
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 8 years ago.
Improve this question
I have to write a program that calculates n times n for the numbers between 1 and 9 using a function.
The output should be like 1, 4, 27, 256...
I can feel I'm very close to finishing it but I just can't figure out what the problem is, here is the code I wrote:
#include <iostream>
using namespace std;
int result, number, n;
void function1()
{
result = number;
for (int x = 1; x < number; x++)
{
result = number*result;
}
}
int main()
{
for (n = 1; n < 10; n++)
{
function1();
cout << result << endl;
system("pause");
return 0;
}
}
Try this :-
#include <iostream>
#include <math>
using namespace std;
int result, number, n;
void function1(int number)
{
int result;
result = pow(number,number);
cout<<result;
}
int main()
{
for (n = 1; n < 10; n++)
{
function1(n);
system("pause");
}
return 0;
}
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 8 years ago.
Improve this question
I'm new to C++ and trying to populate and output a 12x12 array using pseudo-random numbers.
Can anyone see where the code is failing?
Code:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <random>
using namespace std;
const int i = 12;
const int j = 12;
int myArray [i] [j] = {0};
void generateArray();
int main()
{
generateArray();
return 0;
}
void generateArray()
{
srand(1234);
for(int i=0; i < 12; i++);
{
for(int j=0; j < 12; j++);
{
myArray[i][j]= rand();
}
cout << myArray[i][j] << " ";
}
}
Thanks,
Ryan
Your for loops have semicolons after their closing parentheses.
This will effectively treat the for loop as an empty body (and just ignore the loop in general).
Remove them and the code should execute.
Your for loop should look as below.
for(int i=0; i < 12; ++i)
{
for(int j=0; j < 12; ++j)
{
myArray[i][j]= rand();
cout << myArray[i][j] << " ";
}
cout << endl;
}
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 8 years ago.
Improve this question
I have to parallelize a program with OpenMP and I don't have any idea. The code below is a similar (very) semplified problem. I have a class whose attributes are a vector and its lenght. The method 'work' calculate each new element v[i] of the vector as the average value of the one before and the one after (considering periodic boundaries, ie element 0 is the average of element 1 and element (len-1)).
class:
#include<vector>
#include<iostream>
class A{
private:
std::vector<int> v;
int len;
public:
A(): len(0), v(0){
v[0] = 0;
}
A(unsigned n): len(n), v(n){
for(int i = 0; i < len; i++)
v[i] = 2*(i+1);
}
void work(){
std::vector<int> temp(len);
for(int i = 0; i < len; i++)
temp[i] = (v[((i-1+len)%len)] + v[((i+1)%len)]) / 2;
v.swap(temp);
}
void out(){
for(int i = 0; i < len; i++)
std::cout << v[i] << " ";
std::cout << std::endl;
}
~A(){}
};
main:
#include <iostream>
#include "omp.h"
#include "class.cpp"
int main () {
A a(5);
for(int i = 0; i < 10; i++){
a.work();
}
a.out();
return 0;
}
The method work is called multiple times. Can someone write me few lines of code to explain what to do?
I have the solution, in the method work of the class you have to write before the cycle:
#pragma omp parallel for
that's all!!!