Pointer to vector - c++

I've got this code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> *vecptr;
int veclen;
void getinput()
{
string temp;
for(int i = 0; i < 3; i++)
{
cin>>temp;
vecptr->push_back(temp);
}
veclen = vecptr->size();
}
int main()
{
getinput();
for(int i = 0; i < veclen; i++)
{
cout<<vecptr[i]<<endl;
}
return 0;
}
My compiler(G++) throw me some errors: test2.cpp:28:17: error: no match for 'operator<<' in 'std::cout << *(vecptr + ((unsigned int)(((unsigned int)i) * 12u)))' ...
What's wrong? What can I do to fix it?

The program is still not completely right. You have to initialize the vector pointer and then give it a size and the use it. A full working code could be,
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> *vecptr = new vector<string>(10);
int veclen;
void getinput()
{
string temp;
for(int i = 0; i < 3; i++)
{
cin>>temp;
(*vecptr)[i] = temp;
}
veclen = (*vecptr).size();
}
int main()
{
getinput();
for(int i = 0; i < veclen; i++)
{
cout<<(*vecptr)[i]<<endl;
}
return 0;
}
Although I have mentioned the size as 10 you could make it a variant.

You need to dereference vecptr here to get the underlying vector:
cout << (*vecptr)[i] << endl;
You will also need to initialize vecptr.

Related

How to read a 2d triangle array from txt file?

I want to read 2d triangle array from a txt file.
1
8 4
2 6 9
8 5 9 6
I wrote this code. At the end I wanted to print it out if I got the array right. When I run it it does not print the array, but in debug it prints. So there is a problem, but I cannot find it. Sometimes it gives segmentation fault, but I dont understand.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
int main() {
std::ifstream input_file("input_file.txt");
int size{1};
int **arr = (int**)malloc(3*sizeof(int));
int *lineArr = (int*) malloc(size*sizeof(int));
int temp{};
int index{};
while(input_file >> temp){
lineArr[index] = temp;
index++;
if(index == size){
index = 0;
arr[size-1] = new int[size-1];
for(int i{}; i<size; i++){
arr[size-1][i] = lineArr[i];
}
size++;
lineArr = (int*) realloc(lineArr, size*sizeof(int));
}
}
input_file.close();
for(int a{}; a<size-1; a++){
for(int j{}; j<=a; j++){
std::cout << arr[a][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
You can just use vector instead of malloc. Like this:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
int main() {
ifstream input_file("input_file.txt");
vector<string> numbers;
if (input_file.is_open()) {
string line;
while (getline(input_file, line)) {
numbers.push_back(line);
}
input_file.close();
}
for (vector<string>::iterator t=numbers.begin(); t!=numbers.end(); ++t)
{
cout<<*t<<endl;
}
return 0;
}

C++ Array of random numbers

I have a bit of a problem with this. I've tried to create a function to return a random number and pass it to the array, but for some reason, all the numbers generated are "0".
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int generLosNum(int);
int main()
{
srand(time(NULL));
int LosNum;
const int rozmiar = 10;
int tablica[rozmiar];
for(int i=0; i<rozmiar; i++)
{
tablica[i] = generLosNum(LosNum);
cout << tablica[i] <<" ";
}
return 0;
}
int generLosNum(int LosNum)
{
int LosowyNum;
LosowyNum = (rand() % 10);
return (LosNum);
}
So the return for your int generLosNum(int LosNum) was printing 0 because you had it returning LosNum which was initialized equaling to zero. I changed your code so it works and will print out the 10 random numbers.
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int generLosNum();
int main()
{
srand(time(NULL));
int LosNum = 0;
const int rozmiar = 10;
int tablica[rozmiar];
for (int i = 0; i < rozmiar; i++)
{
tablica[i] = generLosNum();
cout << tablica[i] << " ";
}
return 0;
}
int generLosNum()
{
int LosowyNum;
LosowyNum = (rand() % 10);
return LosowyNum;
}
Change your method generLosNum to the following and the method signature to int generLosNum() and it should work.
int generLosNum()
{
return (rand() % 10);
}
Reason: As others also mentioned in the comments, you were just returning the number that you passed in as parameter and also the logic for this method doesn't even need a parameter.

I cant seem to assign a pointer an array and then change the contents of the array

I couldn't figure out how to make a function return an array so instead I decided to try and pass an empty array (of the correct size) into my function and than reassign the address to a different array of the same size. Is this at all a way to do things??? Can someone show me what to do? if this is wrong can you fill me in on how to do this?
here is my code:
#include <iostream>
#include <cmath>
using namespace std;
void ArrayFiller(int earray,int s, int f){
int *ptrarray = &earray;
int prenum_size = std::abs(s) + f - 1;
int pre_num[prenum_size];
for(int x=s;x<f;x++){
pre_num[x+std::abs(s)] = x;
}
*ptrarray = pre_num;
}
int Main(){
int first = -10;
int second = 15;
int temp[abs(first) + abs(second)];
ArrayFiller(temp, first, second);
int n = sizeof(temp)/sizeof(temp[0]);
for (int i = 0; i < n; i++) {
cout << temp[i] << ' ';
}
return 0;
}
I think you're looking for something like this:
#include <iostream>
#include <cmath>
using namespace std;
void ArrayFiller(int earray[],int s, int f){
for(int x=s;x<f;x++){
earray[x+(std::abs(s))] = x;
}
}
int main(){
int first = -10;
int second = 15;
int n = abs(first)+abs(second);
int* temp = new int[n];
ArrayFiller(temp, first, second);
for (int i = 0; i < n; i++) {
cout << temp[i] << ' ';
}
delete [] temp;
return 0;
}

Passing array as an argument from one function to another in C++?

I have a program where I want to pass an array - in this case k[arrsize], which is a parameter of the funciton fillArray() to the other function create_file() (the same array, filled with the random numbers). However, I cannot pass the same array and I would like to ask how can this be done?
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <time.h>
#include <stdlib.h>
using namespace std;
const int arrsize = 20;
//int a[arrsize];
fstream p;
void fillArray(int k[arrsize])
{
srand((unsigned)time(NULL));
for (int i = 0; i<20; i++)
{
if (i % 2 == 0)
{
k[i] = -(rand() % 100);
}
else
{
k[i] = (rand() % 100);
}
}
}
void create_file(int k[arrsize])
{
p.open("danni.dat", ios::out);
for (int i = 0; i<20; i++)
{
p << k[i] << endl;
}
p.close();
}
int main() {
fillArray(k);
create_file(k);
return 0;
}
You simply forget to define an array:
int main() {
int k[arrsize];
fillArray(k);
create_file(k);
}
Usually you don't want to pass the whole array, instead you might want to pass a reference to it. I suggest you to use std::array instead of a C-style arrays.
#include <array>
void fill(std::array<int, 1>& a)
{
a[0] = 0;
}
int main()
{
std::array<int, 1> a = {};
fill(a);
return 0;
}

pointers to multidimensional arrays in class

I tried to write a program but I get segmentation fault (core dumped) while running . When I put a defined array like array_2d[10][1] , the problem is solved but I need to do the memory allocation for my project. It is a simple version of my code:
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
class Exam
{
private:
double** array_2d;
unsigned int num;
unsigned int num1;
public:
Exam();
void memoryallocation();
void show();
};
Exam::Exam()
{
num=10;
num1=1;
}
void Exam::memoryallocation ()
{
double** array_2d = new double*[num];
for (unsigned int i = 0; i < num ;i++)
{
array_2d[i] = new double[num1];
}
}
void Exam::show ()
{
ifstream file;
file.open("fish.txt");
for (unsigned int i = 0; i < num; i++)
{
for (unsigned int j = 0; j < num1; j++)
{
file >> array_2d[i][j];
cout<<array_2d[i][j]<<" ";
}
cout<<endl;
}
file.close();
}
int main()
{
Exam E;
E.memoryallocation();
E.show();
return 0;
}
Inside the function Exam::memoryallocation (), you are declaring array_2d again.
void Exam::memoryallocation ()
{
array_2d = new double*[num]; //remove the redeclaration of array_2d
for (unsigned int i = 0; i < num ;i++)
{
array_2d[i] = new double[num1];
}
}