-Need Help- c++ double function

I'm having some trouble with this program, I'm aware I can use a vector but I'm trying to do it only using arrays. Once the program gets to the initial array size of 1000, it should double the array (in this case to 2000) after copying the data. So for example if I had a list of 3000 names, it'd double once at 1000, then again at 2000 - making the total list 4000. Can someone help me with this? I feel like I get that I have to make a temp/new array with the doubled size and the copied data, but then have the old array point to it. Or something close to that... I have to do the same for halfing the array (if the number f array elements is 1/4 the size), but waiting to get the double correct first to figure out that one (since my array being too small will never be a problem) Thanks for the help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

struct Information {
	char functionality;
	string SSN;
	string name;
};

Information* person;
int numPeople = 1000;
int numRetrieved = 0;
int numArray = 0;
int numInserted = 0;
int numDeleted = 0;


void doubleArray(Information* person){
	numPeople = numPeople * 2;
	Information* temp = new Information[numPeople];
	for(int i = 0; i < numArray; i++){
		temp[i].name = person[i]->name;
		temp[i].SSN = person[i]->SSN;
	}
	delete[] *person;
	*person = temp;
}

void halfArray(Information *person){
	numPeople = numPeople / 2;
}

void deleteInfo(Information *person, string SSN){
	for(int i = 0; i < numArray; i++){
		if(person[i].SSN == SSN){
			for(int k = i; k < numArray-1; k++){
				person[k].SSN = person[k+1].SSN;
				person[k].name = person[k+1].name;
			}	
			numArray--;
			numDeleted++;
			
			if((numArray+1) < (numPeople / 4)){
				//halfArray(person);
			}
		}
	}
}

void retrieve(Information *person, string findSSN, int lastPerson){
	for(int i = 0; i < lastPerson; i++){
		if(person[i].SSN == findSSN){
			numRetrieved++;
		}
	}
}

void insert(Information *person, string SSN, string name){
	if(numArray == (numPeople - 1)){
			doubleArray(person);
	}
	
	bool dontInsert = false;
	for(int i = 0; i <= numArray; i++){
		if(person[i].SSN == SSN){
			dontInsert = true;	
		}
	}
	
	if(dontInsert){
		dontInsert = false;
	}else{
		person[numArray].SSN = SSN;
		person[numArray].name = name;
		numArray++;
		numInserted++;
	}
}

int main(int argc, char* argv[]) {
	person = new Information[numPeople];
	char firstLetter;
	string SSN, firstName, lastName, name; 
	fstream input(argv[1]);
	
	for(int i = 0; !input.eof(); i++){
		input >> firstLetter >> SSN >> firstName >> lastName;
		name = firstName + " " + lastName;
		
		switch(firstLetter){
			case 'd':{
				deleteInfo(person, SSN);
				break;
			}
			case 'i':{
				insert(person, SSN, name);
				break;
			}
			case 'r':{
				retrieve(person, SSN, numArray);
				break;
			}
		}
	}
	input.close();

}
Last edited on
Ok,
Lines 12-17 should not be global, maybe you should think about putting them into the Information class.
Lines 31-33 are:
1
2
3
void halfArray(Information *person){
	numPeople = numPeople / 2;
}

Why does this function take a pointer to an Information?
It is not used in the function.
Also, on line 12 I think you want to write:
Information* temp = new Information[numPeople];
You were declaring this on line 22 inside a function. The array of Informations were deleted as soon as the function returned.

Does this help?
I haven't even touched the halfArray yet (never actually gets called in the main code anyways, because I commented out where I call it). My question is mainly (only place that's stumping me) towards the doubleArray where the list needs to get bigger (but keep the same name so that the program runs with it).

Line 12 I had in my main at one point, I've been switching it around trying to figure out/play with different "solutions" - none of which have worked thus far - so I came here to ask for a bit of assistance.

Let me test around with what you said and I'll edit/reply again, thanks.
1
2
3
4
void doubleArray( Information * person ) {
	Information * temp;
	*person = temp;
}

First, your compiler should call the line 3 an error. You are assigning a value with type Information* into object with type Information.
The *person is same as *(person+0) is same as person[0]. While person is a pointer, the pointed to type is not.


Then the main matter. There are two types of function arguments: by value and by reference.

A reference argument is just a name alias for an object that the caller has. Function can modify caller's object via the reference.

A by value argument is essentially a local variable of the function. It is initialized to be a copy of caller's data and it is destructed at the end of the function.

The person in doubleArray() is just a copy made from caller's variable. You can change the person (i.e. change the address stored in the pointer) but that will have no effect on the caller.

What you should do, is to pass a reference to the pointer as argument.


Your code has other issues too, but start with these. Pay attention to the compiler messages.
Topic archived. No new replies allowed.