Need help soon with C++ program using pointers and arrays

In summary, the conversation discusses a program that reads in a series of numbers and displays how many are greater than their average. There are several problems with the program, including incorrectly using pointers and arrays and not properly copying data when expanding the array size. The conversation also touches on the concept of memory management and the use of memcpy() or a for loop to copy data.
  • #1
twotaileddemon
260
0
Okay so I got the majority of my program complete, however when I try to run it it doesn't display the proper value of the average. I think I somehow messed up using my pointers and arrays, but I can't locate the problem and I've spent hours trying to fix it to no avail. see comments for better description of program.

-------

#include <iostream>

using namespace std;

/* Write a program to read in a series of numbers and display how many
are greater than their average. The user will indicate that there are no more numbers.
by entering a -999. This lab requires that you use new and delete. */

int main ( )
{
// Declaration of variables
int index = 0;
int count = 0;
int amount = 0;
int sum = 0;
int capacity = 2;
int * number;
number = new int[capacity];

// Puts the numbers the user enters into an array
// Keeps track of their sum;
// Keeps track of the numbers entered;
while(true)
{
cout <<"Enter a number: ";
cin >> number[index];

if (number[index] == -999)break;

sum = sum + number[index];
amount++;

if (index == capacity)
{
int * tmp = new int[capacity];
number = tmp;
delete []tmp;
}

index++;
}

// Accumulator variable to count numbers larger than average
for (index = 0; index <= amount; index++)
{
if (number[index] > (sum / amount)) count++;
}

// Output
cout << "There are " << count
<< " numbers greater than their average." << endl;


return 0;
}
 
Technology news on Phys.org
  • #2
Several problems,
You create an array with 2 items, but you use elements greater than 0 or 1.
You create a new array to exapand it, but the new array is the same size and you don't copy the data to the new array you copy the pointer.
You create an array, copy the pointer and then delete the array, the memory the number pointer points at is no longer valid.

You have most of the ideas correct,
Allocate an array of a reasonable size 100 or 1000 entries if the user is entering them by hand.
Keep track of how many number have been entered and if you have to allocate more space make a new array twice the size of the old one and copy the data using either memcpy() or just looping through the values.

ps. Be careful about the result of dividing one integer by another.
 
Last edited:
  • #3
At the office, if you run out of filing cabinet space, you might seek to increase your capacity. What's wrong with the following directions for doing so?

1. Acquire new filing cabinet.
2. Install new filing cabinet.
3. Put old filing cabinet in the dumpster.

(Hint: think very literally)
 
  • #4
mgb_phys said:
Several problems,
You create an array with 2 items, but you use elements greater than 0 or 1.
You create a new array to exapand it, but the new array is the same size and you don't copy the data to the new array you copy the pointer.
You create an array, copy the pointer and then delete the array, the memory the number pointer points at is no longer valid.

You have most of the ideas correct,
Allocate an array of a reasonable size 100 or 1000 entries if the user is entering them by hand.
Keep track of how many number have been entered and if you have to allocate more space make a new array twice the size of the old one and copy the data using either memcpy() or just looping through the values.

ps. Be careful about the result of dividing one integer by another.
hm.. I think I can go on this. I'm supposed to start with 2 "elements" for the array and allocate 2 more spaces for the "elements" each time they are filled up...
and I'm supposed to use pointers or something.

I can change them to double no problem though.
 
  • #5
Even if you only allocate 2 extra elements at a time, you still have to copy the old values to the new array before deleting the old array.
 
  • #6
mgb_phys said:
Even if you only allocate 2 extra elements at a time, you still have to copy the old values to the new array before deleting the old array.

do I use pointers to do this?

EDIT: "memcpy() or just looping through the values."
I've never used memcpy() before and am unfamiliar with it. Should I use a for loop to copy the old values? How do I go about doing this?

if (index == capacity)
{
capacity = capacity + 2;
int * tmp = new int[capacity];
number = tmp;
delete []tmp;
}

I added capacity = capacity + 2 each time this if loop runs through, that way when the whole for loop repeats, it has allocated 2 more memory.. I think. Not sure about how to copy data though.
 
  • #7
You should be able to access the values of the old array, copy them in, as they exist in the memory, and then delete it after the for loop, as you would if you were to take a pointer to an int value and store it another pointer to int, and then delete it. So yes it's possible
 

What is the purpose of pointers and arrays in C++?

Pointers and arrays are essential data types in C++ that allow for efficient manipulation and storage of data. Pointers are variables that store the memory address of other variables, while arrays are collections of variables of the same data type stored in consecutive memory locations. They are frequently used for dynamic memory allocation and passing data between functions.

How do I declare and initialize a pointer in C++?

To declare a pointer, you use the asterisk (*) symbol before the variable name. For example, int *ptr; declares a pointer named ptr that points to an integer. To initialize a pointer, you can assign it the address of another variable using the address-of operator (&), such as ptr = # where num is an integer variable.

How do I access and manipulate the values stored in an array using pointers?

Pointers provide a convenient way to access and modify the elements of an array. You can use the dereference operator (*) to access the value stored at the memory address pointed to by a pointer. For example, *ptr returns the value stored in the first element of the array. To access other elements, you can use pointer arithmetic, where adding an integer value to a pointer moves it to the next element in the array.

Can I use pointers and arrays together in C++?

Yes, pointers and arrays are often used together in C++ to create dynamic arrays. By dynamically allocating memory using pointers, you can create arrays of any size at run time, unlike fixed-size arrays declared using square brackets ([]). Pointers can also be used to pass arrays as arguments to functions, allowing for efficient manipulation of data without having to copy the entire array.

What are some common mistakes to avoid when using pointers and arrays in C++?

Some common mistakes to avoid include dereferencing uninitialized pointers, which can lead to unexpected behavior or even crashes, and using out-of-bounds indexing for arrays, which can result in data corruption. It is also important to properly allocate and deallocate memory when using dynamic arrays to prevent memory leaks. Additionally, it is essential to understand the difference between arrays and pointers and how they are stored in memory to avoid confusion when working with them.

Similar threads

  • Programming and Computer Science
Replies
5
Views
884
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top