Filling an 2D array with pseudo-random numbers [closed] - c++

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;
}

Related

Segfault when puting cin value into array [closed]

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
Here is all of my code:
#include <fstream>
#include <vector>
#include <algorithm>
#include <set>
#include <iostream>
using namespace std;
int main(){
ifstream fin("cereal.in");
ofstream fout("cereal.out");
int n, m, f, s;
cin >> n >> m;
int c1[n];
int c2[n];
for(int i = 0; i < n; i++){
cin >> c1[i] >> c2[i];
c1[i]--;
c2[i]--;
}
vector<int> fm(m, -1);
set<int> fs;
vector<int> ans;
for(int i = n-1; i >= 0; i++){
if(fs.find(c1[i]) == fs.end()){
fs.insert(c1[i]);
}else{
if(fs.find(fm[c1[i]]) == fs.end()){
fs.insert(fm[c1[i]]);
}
}
fm[c1[i]] = c2[i];
ans.push_back(fs.size());
}
for(int i = 0; i < n; i++){
cout << ans[i] << endl;
}
}
In the first for loop when i = n-1, I'm getting a Segmentation fault: 11. I used a cout call after the cin in that for loop to find this information.
What is the reason for this? It was working fine a couple of minutes ago and I didn't even touch this part of the code and it stopped working. I'm using VS Code, but even when I use an online compiler it doesn't work. Here is the input I am giving:
4 2
1 2
1 2
1 2
1 2
Please help me. This isn't the first time I'm having a problem like this.
for(int i = n-1; i >= 0; i++){ // <------
if(fs.find(c1[i]) == fs.end()){
fs.insert(c1[i]);
}else{
if(fs.find(fm[c1[i]]) == fs.end()){
fs.insert(fm[c1[i]]);
}
}
fm[c1[i]] = c2[i];
ans.push_back(fs.size());
}
Change to i--. You're accessing c1[i] with out of bound index.
You have an array index out-of-bounds bug, causing the program to access data where it shouldn't.
for(int i = n-1; i >= 0; i++){
In the above statement the integer i wraps around until it overflows, thus indexing c1 into forbidden regions of memory.
The OS is alerted of the memory violation and killed the misbehaving program. When this happens the program is said to encounter a segmentation fault.

Filling vector with primes in a certain way [closed]

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?

How do you use nested for loops to make triangles in c++ [closed]

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
Here's what I have:
for (int i = 0 ; i <= height; i++) {
for (int s = 0; s < height; s++) {
cout << "*" << endl;
}
}
It prints out a list of asterisks like this:
*
*
*
*
You're outputting a newline after every single character. You only want to do that in the outer loop, after each line of characters.
In addition, using a fixed value for the width will result in a square rather than a triangle. You should therefore base your inner loop on i rather than height:
#include <iostream>
using namespace std;
int main (void) {
int height = 7;
for (int i = 1 ; i <= height; i++) {
for (int s = 0; s < i; s++)
cout << "*";
cout << endl;
}
return 0;
}
The output of that is the very triangular:
*
**
***
****
*****
******
*******
Of course, that will work but it's hardly embracing the essence of the C++ language.
What you really have there is C code but using C++ output methods, something that will get you quickly labeled as a C+ programmer :-)
Although I wouldn't hand this in as a beginner assignment, it will at least force you to think about using the added facilities of the C++ language going forward:
#include <iostream>
using namespace std;
int main (void) {
int height = 7;
string s(height, '*');
for (int i = 1 ; i <= height; i++)
cout << s.substr(0,i) << '\n';
return 0;
}
That thinking will convince you at some point to discard character arrays for strings (or arrays in general for vectors, or many other similar decisions), something that will make your programming much faster and safer in future.
try this
int main()
{
int height =10;
for (int i = 0 ; i <= height; i++) {
for (int s = 0; s < i; s++) {
cout << "*";
}
cout << endl;
}
}

C++ program calculates n times n for the numbers 1 to 9 [closed]

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;
}

OpenMp example program [closed]

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!!!