struct Student
{
char* name;
int balls;
};
void inputdata(Student **s, int *n)
{
int nn;
printf("%s\n", "Input amount of students");
scanf("%i", &nn);
Student* a = new Student[nn];
for (int i = 0; i < nn; ++i)
{
scanf("%s", &a[i].name);
scanf("%i", &a[i].balls);
}
n = &nn;
s = &a;
}
void print(Student **s, int n)
{
for (int i = 0; i < n; ++i)
{
printf("%s %i\n", s[i]->name, s[i]->balls);
}
}
int main(int argc, char const *argv[])
{
Student** s;
int *n;
inputdata(s, n);
print(s, *n);
return 0;
}
So how am I supposed to input data and print data on console screen.
I kinda input data, ok, unable to print it on my screen. Program ends. What am I supposed to fix here?
You should pass pointers to what should be modified in callee functions.
Callee functions should dereference pointers passed to modify what should be modified.
You have to allocate for strings before reading.
It is inconsistent that an array of Student in the function inputdata but an array of Student* is required in the function print.
You should limit the maximum length to read to the number of elements in the buffer minus one to prevent buffer overrun when you use %s. The "minus one" is for terminating null-character.
Fixed code:
#include <cstdio>
struct Student
{
char* name;
int balls;
};
void inputdata(Student **s, int *n)
{
int nn;
printf("%s\n", "Input amount of students");
scanf("%i", &nn);
Student* a = new Student[nn];
for (int i = 0; i < nn; ++i)
{
a[i].name = new char[4096];
scanf("%4095s", a[i].name);
scanf("%i", &a[i].balls);
}
*n = nn;
*s = a;
}
void print(Student *s, int n)
{
for (int i = 0; i < n; ++i)
{
printf("%s %i\n", s[i].name, s[i].balls);
}
}
int main(int argc, char const *argv[])
{
Student* s;
int n;
inputdata(&s, &n);
print(s, n);
return 0;
}
Related
I'm trying to work with dynamic arrays. When I try to overload the "=" operator it does not work. When debugging the file it doesn't execute the void function to overload the operator.
#include <iostream>
using namespace std;
class cppArray {
public:
cppArray(int size);
~cppArray();
int read(int index);
void write(int content, int index);
void operator=(cppArray& s);
int search(int target);
int size();
private:
int* myArray;
int arraySize;
};
cppArray::cppArray(int size) {
myArray = new int[size];
arraySize = size;
}
//delete the memory space assigned to myArray
cppArray::~cppArray() {
delete[] myArray;
myArray = 0;
}
int cppArray::read(int index) {
if (index < arraySize) {
return myArray[index];
}
else {
cout << "Out of range" << endl;
exit(1);
}
}
Here I'm trying to copy the content of the original array to an auxiliar one, and then redefine the size of the original array so I can add more content to the original array
void cppArray::write(int content, int index) {
if (index < arraySize) {
myArray[index] = content;
}
else {
cppArray auxArray(arraySize);
auxArray.myArray = myArray;
delete[] myArray;
arraySize = index + 1;
myArray = new int[arraySize];
myArray = auxArray.myArray;
myArray[index] = content;
}
}
I'm pretty sure this is wrong, but I can't figure out a way to overload it correctly
void cppArray::operator=(cppArray& s) {
delete[] s.myArray;
s.myArray = new int[arraySize];
for (int i = 0; i < arraySize; i++)
{
myArray[i] = s.myArray[i];
}
}
int cppArray::size() {
return arraySize;
}
int main(int argc, char** argv) {
cppArray dsArray(3);
dsArray.write(1, 0);
dsArray.write(2, 1);
dsArray.write(3, 2);
dsArray.write(4, 3);
for (int i = 0; i < dsArray.size(); i++) {
cout << dsArray.read(i) << "\t";
}
cout << endl;
return 0;
}```
Your implementation is almost correct, but you delete the wrong array. You should only modify *this object, and not s. Also, you should follow conventions, or people will be very surprised when using your class.
Here's corrected version:
//return *this - a very expected convention
cppArray& cppArray::operator=(const cppArray& s) {
// Make sure s is not modified ^^^^
if (this == &s) {
return *this; //protection against self writing, things would get bad if you did that
}
arraySize = s.arraySize; //copy size first
delete[] myArray; //delete array from this object
myArray = new int[arraySize]; //recreate array
for (int i = 0; i < arraySize; i++)
{
myArray[i] = s.myArray[i]; //no changes here
}
return *this;
}
edit1: add a running tiny version.
I wrote a cpp file including some classes. When I test it in a single file, everything works but when I link it with other c files, the data I stored in the array in the class changed. I know there must be something wrong with my memory allocation so I changed it into dynamic one using new
but cant figure out where or how to fix
work in single .cpp file
in a file called test.app
class Board
{
public:
int **grid;
bool done;
int result;
public:
Board()
{
grid = new int*[3];
for(int i = 0; i < 3; ++i){
grid[i] = new int[3];
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
grid[i][j]=0;
}
}
done=false;
result=0;
}
}
class Node
{
public:
Board **arr;
//double heuristic;
bool done;
int result;
int prevx, prevy;
int next_turn;
public:
Node()
{
arr = new Board*[3];
for(int i=0;i<3;i++)
{
arr[i] = new Board[3];
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
arr[i][j] = new Board();
}
}
done = false;
//heuristic=0;
result = 0;
prevx = -1;
prevy = -1;
next_turn=1;
}
}
and the code where thing go wrong:
Treenode *head; //treat as global variable
void agent_start( int this_player )
{
//nothing to do
//cout << "here all good" << endl;
head = new Treenode();
//cout << head << endl;
m = 0;
return;
}
int agent_second_move( int board_num, int prev_move )
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(head->arr[res_boardx][res_boardy].grid[cordx][cordy] == 1)
{
cout << "here cant print" << endl;
head->move2(i,j,-1);
cout << "here cant print" << endl;
}
else if(head->arr[res_boardx][res_boardy].grid[cordx][cordy] == -1)
{
cout << "here cant print" << endl;
head->move2(i,j,1);
}
}
}
in test.h
extern int port;
extern char *host;
#ifdef __cplusplus
extern "C" {
#endif
extern char *host;
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
extern "C" {
#endif
// parse command-line arguments
void agent_parse_args( int argc, char *argv[] );
// called at the beginning of a series of games
void agent_init();
// called at the beginning of each game
void agent_start( int this_player );
int agent_second_move(int board_num, int prev_move );
int agent_third_move(int board_num,int first_move,int prev_move);
int agent_next_move( int prev_move );
void agent_last_move( int prev_move );
// called at the end of each game
void agent_gameover( int result, int cause );
// called at the end of the series of games
void agent_cleanup();
#ifdef __cplusplus
}
#endif
in main.cpp
int main(int argc, char *argv[]){
agent_start(1);
int b = agent_second_move(1,1);
}
the output is:
[1] 26904 illegal hardware instruction
or
segmentation fault
before when I delared
class Node
{
public:
Board arr[3][3]; ///
in Node class.
the working version before which caused data in treenode changed
class Board
{
public:
int grid[3][3];
bool done;
int result;
public:
Board()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
grid[i][j]=0;
}
}
done=false;
result=0;
}
}
class Node
{
public:
Board arr[3][3];
//double heuristic;
bool done;
int grid[3][3];
int result;
int prevx, prevy;
int next_turn;
public:
Node()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
arr[i][j] = Board();
grid[i][j]=0;
}
}
done = false;
//heuristic=0;
result = 0;
prevx = -1;
prevy = -1;
next_turn=1;
}
}
Treenode *head;
head = new Treenode();
void print_map(){
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
int res_boardx, res_boardy, cordx, cordy;
res_boardx = (i-1)/3;
res_boardy = (i-1)%3;
cordx = (j-1)/3;
cordy = (j-1)%3;
cout << head->arr[res_boardx][res_boardy].grid[cordx][cordy];
}
cout << endl;
}
}
the 2D array printed out is below when I call print function outside this
file which is wrong since it should either be 1 or 0 or -1.
433000000
107312758200000000
000000000
000000000
000000000
000000000
00000-1000
000000000
000000000
You are not following the rule of 5. You have some memory allocation in constructor, so you need a non default destructor, to correctly free everything, as long as explicit move/copy constructors and assignment operators.
If possible, you should stick to standard containers like std::vector which handle all the corner cases for you.
Header File
#pragma once
#ifndef PLAYERDATA_H
#define PLAYERDATA_H
#include <string>
using namespace std;
class PlayerData
{
private:
Private member variables
static const int SIZE = 10;
string name; //Player Name
int jnum; //Jersey Number
string team; //Player Team
string position; //Player position
int points[SIZE]; // Array of points for last 10 games
int rebounds[SIZE]; // Array of rebounds for last 10 games
int assist[SIZE]; // Array of assist for last 10 games
double ap = 0.0; // Average number of points
double ar = 0.0; // Average number of rebounds
double aa = 0.0; // Average number of assits
public:
Constructor to initialize data if no data is passed
// Constructor #1
PlayerData()
{
jnum = 0;
name = "";
team = "";
position = "";
for (int i = 0; i < SIZE; i++)
{
points[SIZE] = 0;
rebounds[SIZE] = 0;
assist[SIZE] = 0;
}
}
// Constructor #2
Constructor to accept parameter. Collects jersey number, name, team name, position, array of points for last 10 games, array of rebounds for last 10 games, array of assist for last 10 games.
PlayerData( int jn, string n, string t, string pos, int p[SIZE], int r[SIZE], int a[SIZE])
{
jnum = jn;
name = n;
team = t;
position = pos;
for (int i = 0; i < SIZE; i++)
{
points[SIZE] = p[SIZE];
rebounds[SIZE] = r[SIZE];
assist[SIZE] = a[SIZE];
}
}
// Mutator Function
void setJersery(int jn)
{
jnum = jn;
}
void setName(string n)
{
name = n;
}
void setTeam(string t)
{
team = t;
}
void setPosition(string pos)
{
position = pos;
}
void setPoints(int p[SIZE])
{
for (int z = 0; z < SIZE; z++)
{
points[SIZE] = p[SIZE];
}
}
void setRebounds(int r[SIZE])
{
for (int z = 0; z < SIZE; z++)
{
rebounds[SIZE] = r[SIZE];
}
}
void setAssist(int a[SIZE])
{
for (int z = 0; z < SIZE; z++)
{
assist[SIZE] = a[SIZE];
}
}
// Acessor methods
string getName()
{
return name;
}
int getJersey()
{
return jnum;
}
string getTeam()
{
return team;
}
string getPosition()
{
return position;
}
int getPoints()
{
return points[SIZE];
}
int getRebounds()
{
return rebounds[SIZE];
}
int getAssist()
{
return assist[SIZE];
}
/*
double averageP(int p[], const int SIZE);
double averageR(int r[], const int SIZE);
double averageA(int a[], const int SIZE);
*/
void averageP(int p[], const int SIZE);
void averageR(int r[], const int SIZE);
void averageA(int a[], const int SIZE);
double getAP()
{
return ap;
}
double getAR()
{
return ar;
}
double getAA()
{
return aa;
}
};
#endif // !PLAYERDATA_H
Calculates average points,rebounds, assist from the arrays that were passed.
PlayerData.cpp
#include "PlayerData.h"
using namespace std;
// Calculate average points
void PlayerData::averageP(int p[], const int s)
{
for (int c = 0; c < s; c++)
{
ap += p[c];
}
ap /= s;
//return ap;
}
// Calculate average rebounds
void PlayerData::averageR(int r[], const int s)
{
for (int c = 0; c < s; c++)
{
ar += r[c];
}
ar /= s;
//return ar;
}
// Calculate average assist
void PlayerData::averageA(int a[], const int s)
{
for (int c = 0; c < s; c++)
{
aa += a[c];
}
aa /= s;
//return aa;
}
Main
#include <iostream>
#include <iomanip>
#include "PlayerData.h"
using namespace std;
int main()
{
const int SIZE = 10;
int points[SIZE] = { 10,10,10,10,10,10,10,10,10,10 };
int assist[SIZE] = { 2,2,2,2,2,2,2,2,2,2, };
int rebounds[SIZE] = { 3,3,3,3,3,3,3,3,3,3 };
Here is where the problem occurs. The compiler marks under the 6 as if the int is not part of the arguments for the constructor. I'm not sure why it is doing this. I receive this message "No instance of constructor "PlayerData::PlayerData" matches the argument list."
PlayerData player1(6, "Jimmy Butler", "Chicago Bulls", "Forward", points[SIZE], rebounds[SIZE], assist[SIZE]);
getchar();
return 0;
}
Constructor requires an array of integers and in main you are passing a pointer to int. If you want to pass the whole array you should delete the [SIZE] because that is translated as (if SIZE is 5 for example) "give me the 6th element of 5 element array".
Try calling it like this.
PlayerData player1(6, "Jimmy Butler", "Chicago Bulls", "Forward", points, rebounds, assist);
This is my class
class Process {
public:
Process();
void createBurstArray(int *bursts, int sizeOfArray);
void createIOArray(int *IO, int capacity);
int *burstArray;
int *ioArray;
int currentBurst;
int currentIO;
int currentState;
};
Process::Process()
{
}
void Process::createBurstArray(int *bursts, int capacity){
burstArray = new int[capacity];
burstArray = bursts;
};
void Process::createIOArray(int *IO, int capacity) {
ioArray = new int[capacity];
ioArray = IO;
for (int i = 0; i < capacity; i++)
};
void main(){
int processOneBursts[7] = { 12,10,15,11,9,10,11 };
int processOneIO[6] = { 44,52,21,42,31,77 };
Process processes[9];
Process one;
processes[0] = one;
one.createBurstArray(processOneBursts, 7);
one.createIOArray(processOneIO, 6);
}
When I try accessing the ioArray
one.ioArray[1]
I get the value stored in the ioArray at index 1, butw hen I try accessing the ioArra through my object array index it doesn't work:
for (int i = 0; i < 9; i++) {
cout << processes[i].ioArray[i] << endl;
}
What am I doing wrong?
#include <iostream>
using namespace std;
class Process {
public:
// Process();
void createBurstArray(int *bursts, int sizeOfArray);
void createIOArray(int *IO, int capacity);
int *burstArray;
int *ioArray;
int currentBurst;
int currentIO;
int currentState;
};
int main()
{
Process p[10];
for (int i = 0; i < 9; i++) {
p[i].ioArray = new int[1];
}
for (int i = 0; i < 9; i++) {
p[i].ioArray[0] = i;
}
for (int i = 0; i < 9; i++) {
cout << p[i].ioArray[0] << endl;
}
return 0;
}
This works, i think you didn't initialize the dynamic array.
You are object of the class with default constructor which means all the elements in the class will be uninitialized including the element int *ioArray;.
And you are trying to access ioArray in the for loop which is road set to segmentation fault.
To correct this, you must initialize the element before using them.
Also, Your below function is problematic.
void Process::createIOArray(int *IO, int capacity) {
ioArray = new int[capacity];
ioArray = IO;
}
This is a memory leak, since you have assign ioArray to IO ,new int[capacity] memory will not be freed.
Solution:
#include <iostream>
using namespace std;
class Process {
public:
Process();
void createBurstArray(int sizeOfArray);
void createIOArray(int capacity);
int *burstArray;
int *ioArray;
int currentBurst;
int currentIO;
int currentState;
// Don't forget to delete burstArray, ioArray in destructor.
};
void Process::createIOArray(int capacity) {
ioArray = new int[capacity];
};
void Process::createBurstArray(int sizeOfArray) {
burstArray = new int[sizeOfArray];
};
Process::Process(){
createIOArray(10);
createBurstArray(10);
// Similarly you have to initialize other members of the class
}
int main()
{
Process processes[9];
for (int i = 0; i < 9; i++) {
cout << processes[i].ioArray[i] << endl;
}
}
I was looking for some help in sorting an Array. I have to do it this way, but I am getting some error messages I am not understanding such as:
[WARNING name lookup of 'index' changed .
matches this 'char* index[const char*, int] undr ISO standard rules
matches this index under old rules
invalid types 'int&[char()(const char*, int)] for array subscript
at global scope
I have some suspicion of what it is but a little lost of how to fix it.
The file I am opening is sentence that reads: Oliver was a Golden Retreiver whose fur was long and golden.
As you can tell I am a complete beginner so any tips will be greatly appreciated Thanks in advance!
#include <iostream>
#include <fstream>
using namespace std;
void swap_values(int& v1, int& v2);
int index_of_smallest(int list[],int start_index, int number_used);
void initialize(int list[]);
void Sort(int list[],int&num);
void characterCount(char ch, int list[]);
void readText(ifstream& intext, char& ch, int list[]);
void totalCount(int list[]);
int main()
{
int index,letterCount[26];
char ch;
ifstream inFile;
ofstream outFile;
cout<<"This is the text of the file:"<<endl;
outFile.open("C:/temp/Data_Chapter_7_8.txt");
if(!outFile)
{
cout<<"Cannot open file."<<endl;
}
inFile.open("C:/temp/Data_Chapter_7_8.txt");
if (!inFile)
{
cout << " Cannot open file." <<endl;
}
initialize(letterCount);
inFile.get(ch);
while (inFile.get(ch))
{
int index;
readText(inFile,ch,letterCount);
index++;
}
totalCount(letterCount);
inFile.close();
system("PAUSE");
return 0;
}
void initialize(int list[])
{
for(int x = 0;x<26;x++)
list[x] = 0;
}
void characterCount (char ch, int list[])
{
ch = tolower(ch);
if(static_cast<int>(ch)>=97&&(static_cast<int>(ch)<=122))
list[static_cast<int>(ch)-97]++;
}
void readText(ifstream& intext, char& ch, int list[])
{
if (ch != '.')
{
characterCount (ch,list);
}
}
void totalCount(int letterCount[])
{
for(int x=0;x<26;x++)
if(letterCount[x]>0)
cout<<static_cast<char>(x+97)<<" "<<letterCount[x]<<endl;
}
void Sort(int list[], int number_used)
{
int index_of_next_smallest;
for(int index= 0; index<number_used -1; index++)
index_of_next_smallest = index_of_smallest(list, index,number_used);
swap_values(list[index],list[index_of_next_smallest]);
}
}
int index_of_smallest(int list[], int start_index, int number_used);
{
int min = list[start_index];
index_of_min = start_index;
for (int index= start_index + 1; index < number_used; index++)
if (list[index]>min)
{
min = list[index];
index_of_min = index;
}
return index_of_min;
}
void swap_values(int& v1, int& v2)
{
int temp;
temp = v1;
v1 = v2;
v2 = temp;
}
Inside your Sort function, change
for(int index= 0; index<number_used -1; index++)
to
int index;
for(index = 0; index < number_used-1; index++)
because you need to access index after the loop ends.