|
-
Sep 18th, 2002, 05:57 PM
#1
Thread Starter
Addicted Member
Help on Pointers project Please!
I have this project to do and I have no idea where to even start let alone do the dang thing. Heres the project:
Objective: This program will consist of creating a program that will accept a list of integers from the user, perform operations on the list and return results to the user
Specifics: The program should initially ask the user for a list of integers. The program should continuously request integers until the user inputs a negative number. Once the list is read in, the program should then present the user with a list of options. The options should include: input a new list, display the maximum, display the minimum, display the average, display the median, and exit. Once an operation is performed, the menu should be redisplayed.
Requirements:
- The integers must be stored in a dynamic array, which is initially 10 items in size and doubles each time more space is needed
- The menu must be implemented with a switch statement; use default condition to handle menu error checking
- Each case of the switch statement, except default, must call a function to process the operation
- All function definitions must be placed after main, through the use of prototypes
- Do not use global variables
OMG, this thing is like impossible.
Last edited by chugger93; Oct 1st, 2002 at 10:17 AM.
-
Sep 18th, 2002, 09:01 PM
#2
Hyperactive Member
Why don't you start doing it first and ask us the parts which you are stuck?
Don't try to code everything in one go(, it is hard to visualise). Do it progressively. After you have made the first part working, then proceed on to code the next part.
I have a similar program like this, but I'm not going to give it to you because you will not learn anything out of it. Try doing it yourself.
-
Sep 18th, 2002, 09:13 PM
#3
Thread Starter
Addicted Member
I dunno even where to start man! Its my friggin 2nd week in C++, teacher goes way to fast. I dunt even really like C++, I just need to pass the class to get my degree
-
Sep 18th, 2002, 09:21 PM
#4
Frenzied Member
Then ask the instructor for help. Most will be more then willing to give you some time if you dont understand something.
Z.
-
Sep 19th, 2002, 11:03 AM
#5
Thread Starter
Addicted Member
can anyone at least get me started?
-
Sep 19th, 2002, 11:15 AM
#6
Monday Morning Lunatic
Take a look at the vector template, www.cplusplus.com should have some information on that. Play around with it for a while, then see how you go...
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 19th, 2002, 11:50 AM
#7
Frenzied Member
This reads them in - but no teacher whio can actually code will believe this is your code Fair warning.
Use *base and current to print the numbers back to the user.
I may have made a mistake or three, but you get to fix it.
Code:
#include <stdio.h>
#include <stdlib.h>
#define zout(c) memset(c,0x00,sizeof(c))
static int *array,*base;
static size_t arrlen=5;
void more(int);
int main(){
int current=0; /* counter for the number of integers in the array */
const char *prompt="Enter a number, <RETURN> to quit: ";
char tmp[20]; /* storage for input */
zout(tmp); /* zero the string */
array=(int *)malloc(sizeof(int)*arrlen); /* create half-size array */
more(current); /*bld the array to start with*/
for(printf(prompt);*gets(tmp);current++,array++){
if(!current%10)more(current);
*array=atoi(tmp);
zout(tmp);
printf(prompt);
}
return 0;
}
void more(int offset){
arrlen*=2; /* array doubles in size each time */
base=realloc(array,arrlen*sizeof(int)); /* set the beginning of the array */
array=base;
if(array){
if(offset){
offset++;
while(offset--) array++; /* reposition the active pointer */
}
}else{
perror("Unable to allocate more memory");
exit(EXIT_FAILURE);
}
return;
}
-
Sep 19th, 2002, 11:54 AM
#8
Monday Morning Lunatic
Ouch
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 19th, 2002, 11:59 AM
#9
Thread Starter
Addicted Member
I cant use a vector dude, he'll wonder about how im knowing about it.
and Jim, *** is that code? Thats like really advanced I think for this assignment...isnt their a less complicated way?!
-
Sep 19th, 2002, 12:05 PM
#10
Monday Morning Lunatic
Well, he can come here and get the same response.
It's the best way, in C++, and therefore is the way that should be taught.
Jim...you're a professional -- would you say that they should use pointers, or use the vector first, and *then* learn how it works inside?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 19th, 2002, 01:10 PM
#11
Frenzied Member
Beautiful code, jim =.). Though no globals.
parksie: vector first, then linked lists, and then make them write a vector. So you get basic pointers, and then array like pointers.
chugger: You could write the code with a vector, debug it, and such, and then re-write the code using a pointer instead of a vector.
Z.
-
Sep 19th, 2002, 02:24 PM
#12
Thread Starter
Addicted Member
WEll, mabe we dont have to use it with a pointer, it doesnt say that anywhere in the assignment anywhere. I just know we havnt gone over what a vector is. But who knows, mabe today he will, so how do u do it with a vector. or at least start it
-
Sep 19th, 2002, 05:08 PM
#13
Frenzied Member
Parksie - learning C++ was hard for me because I had used C too long. And I do not use C++ very often. Which also doesn't help.
A new person is better off learning C++ and the STL and programming at that level as much as possible. IMO.
He should use vectors for the project - except I don't know how to double a vector's storage, and that seems to be a requirement.
I think the teacher wanted heap pointers.
Probably should not have posted the code written that way.
Usually, it's a bad idea because beginners can't read it. But at least I was sure it wouldn't end up verbatim in his/her homework project. And pass as his own code. I really don't mind showing how, but doing someone else's homework isn't even remotely ethical.
Last edited by jim mcnamara; Sep 19th, 2002 at 05:14 PM.
-
Sep 19th, 2002, 09:04 PM
#14
Frenzied Member
vector doubling is easy... =).
Code:
vec.resize(vec.size()*2);
This teacher seems a bit whacked out to me... o.o;
Z.
-
Sep 19th, 2002, 09:54 PM
#15
Thread Starter
Addicted Member
cant use vectors, gotta use pointers he said
-
Sep 19th, 2002, 09:55 PM
#16
Frenzied Member
Shoot him, and give parksie his job =).
Z.
-
Sep 20th, 2002, 03:37 AM
#17
Monday Morning Lunatic
I'll bloody shoot him, and give a trained monkey his job!
Blimey, no wonder professional programmers are so bad
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 20th, 2002, 06:28 AM
#18
Thread Starter
Addicted Member
ya but i guess it can be done
-
Sep 20th, 2002, 06:30 AM
#19
Monday Morning Lunatic
Try a struct with 3 members, a pointer to the start of your memory block, a count of the number of elements you've allocated, and a count of the number that are actually *used*.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 20th, 2002, 08:12 AM
#20
Frenzied Member
Originally posted by parksie
Blimey, no wonder professional programmers are so bad
I wouldnt say that =). Though it really makes you wonder. I guess you can really tell the difference between those for whom coding is a passion, and those who do it for a living in the quality of the work...
Z.
-
Sep 20th, 2002, 09:13 AM
#21
Frenzied Member
We don't hire programmers right out of university. Period. Too often it doesn't work out.
For example, up until 1995, UNM was producing CS students who had only used Amigas. Had no unix, VMS, MVS, or Windows. Insane. IMO.
-
Sep 20th, 2002, 01:06 PM
#22
Frenzied Member
How much actual experience, jim? Does it have to be actual, on-the-job stuff, or can it be side projects, and such?
Z.
-
Sep 25th, 2002, 06:56 PM
#23
Fanatic Member
***?!??
This is not too hard of a project, its a dynamic array:
PHP Code:
int * dynarray = new int [size];
And it doubles everytime you need more space? Say you start with 10, just size*2 it every time! Thats all...
You have a class,
MyArrayClass with the members: int count, int size, int * dynarray
Thats all.
-
Sep 30th, 2002, 08:56 AM
#24
Thread Starter
Addicted Member
well, i got this so far.
int main(int argc, char* argv[])
{
int *ptr;
ptr = new int[10];
int max_array = 10;
int count = 0;
cout << "Enter A List Of Integers: ";
cin >> max_array[count];
while(true)
{
if(max_array == count)
expand(&ptr,max_array);
ptr[count++];
}
void expand(int **ptr, int &max_array)
{
int *temp;
temp = new int[3*max_array];
for(int i=0;i<max_array;i++)
temp[i] = *(*ptr+i); //Changed
delete [] *ptr;
*ptr = temp;
max_array *= 3;
}
Im still lost though, My code is probaly way off. I will pay someone for the code!, lol uggg im soo confused
Last edited by chugger93; Sep 30th, 2002 at 09:36 AM.
-
Sep 30th, 2002, 11:23 AM
#25
Fanatic Member
boooooooooooo, what you really need is to learn the language so that you dont make careless mistakes. Your code doesnt make sense what so ever!!! What EXACTLY are you tring to do?
Here is a few tips i gotta give you:
PHP Code:
int main(int argc, char* argv[])
{
int *ptr;
ptr = new int[10]; // always check the return of new != NULL
int max_array = 10;
int count = 0;
cout << "Enter A List Of Integers: ";
cin >> [color=red]ptr[count];[/color] // the aray is called ptr
// max_array is only the "count" - a number
while(true) // i see an infinite looop here
{
if(max_array == count) // this doesnt make sense? why?
expand(&ptr,max_array); // what you trying to do again
ptr[count++]; // this line doesnt make sense
}
// obviously this function was not written by you.. whatever
// the case maybe, i dont understand what it tries to do... there
// is no user input in your while loop, so nothing will happen
void expand(int **ptr, int &max_array)
{
int *temp;
temp = new int[3*max_array]; // check if (temp != NULL)
for(int i=0;i<max_array;i++)
temp[i] = *(*ptr+i); //Changed
delete [] *ptr; // set ptr =0
*ptr = temp;
max_array *= 3;
}
-
Sep 30th, 2002, 11:50 AM
#26
Thread Starter
Addicted Member
read my very 1st post, thats what im tryin to do
this is the original code, our teacher gave us to get us started using File I/O, but i dunno how to convert it to our assignment
using namespace std;
void expand(int**,int&);
int _tmain(int argc, _TCHAR* argv[])
{
ifstream infile("data.in");
ofstream outfile("data.out");
int *ptr;
ptr = new int[10];
int max_array = 10;
int count = 0;
while(!infile.eof())
{
if(max_array == count)
expand(&ptr,max_array);
infile >> ptr[count++];
}
infile.close();
//Added: a version of Bubble Sort
for(int i=0;i<count;i++)
{
int min = i;
for(int j=i+1;j<count;j++)
if(ptr[j]<ptr[min])
min = j;
int temp = ptr[i];
ptr[i] = ptr[min];
ptr[min] = temp;
}
for(int i=0;i<count;i++)
outfile << ptr[i] << endl;
outfile.close();
return 0;
}
void expand(int **ptr, int &max_array)
{
int *temp;
temp = new int[3*max_array];
for(int i=0;i<max_array;i++)
temp[i] = *(*ptr+i); //Changed
delete [] *ptr;
*ptr = temp;
max_array *= 3;
}
-
Sep 30th, 2002, 12:34 PM
#27
Frenzied Member
Oh dear Jesus! He teaches _TCHAR, and not vector?!
*dies*
Z.
-
Sep 30th, 2002, 12:34 PM
#28
Quite confusing what your teacher gave you.
e.g. you have one function taking to parameters by reference. Yet one paramter is passed in the C-by-ref style (as pointer) and one in the C++ style (as a real reference).
But basically....
Just replace infile with cin, outfile with cout, change the loop condition to a test whether the user entered a negative number and modify the expand function so that it only doubles the array size instead of tripling.
Next put the input request into a seperate function. Write additional functions that do the computations and output the results.
Then fill the main function with the menu. If you browse this forum a little you'll find examples of how to do this, e.g. the "Input Valid Data" thread on this page.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 30th, 2002, 12:54 PM
#29
Thread Starter
Addicted Member
I keep getting this error msg
C:\Program Files\Microsoft Visual Studio\assign3\assign3.cpp(34) : error C2601: 'expand' : local function definitions are illegal
whenever i try to compile ?
-
Sep 30th, 2002, 12:57 PM
#30
Sounds like a missing closing brace ( } )
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 30th, 2002, 01:03 PM
#31
Thread Starter
Addicted Member
Ok, I just need help on making a function that reads in the integers and once the integers are read in, will present the user with the list of options.
I can do the switch statement, or case statement, Im just not sure how to make a function that reads the integers in, or where to put it.
heres my code so far
using namespace std;
void expand(int**,int&);
int main(int argc, char* argv[])
{
int *ptr;
ptr = new int[10];
int max_array = 10;
int count = 0;
while(!cin.eof())
{
cout << "enter";
cin >> count;
if(max_array == count)
expand(&ptr,max_array);
cin >> ptr[count++];
}
return 0;
}
void expand(int **ptr, int &max_array)
{
int *temp;
temp = new int[2*max_array];
for(int i=0;i<max_array;i++)
temp[i] = *(*ptr+i); //Changed
delete [] *ptr;
*ptr = temp;
max_array *= 2;
Last edited by chugger93; Sep 30th, 2002 at 01:29 PM.
-
Oct 1st, 2002, 06:19 AM
#32
cin won't ever have a eof condition unless the user figures out how to input an EOF character.
The loop condition must be a test for a negative number! It is explicitly stated in your requirements.
And a do...while loop would fit better here (unless you feel uncomfortable with it).
Then you should output a newline or at least whitespace together with the "enter".
The code for entering numbers should be in a seperate function as it may be called from the menu,
As for menus I already told you: browse the forum.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 1st, 2002, 06:26 AM
#33
Monday Morning Lunatic
Originally posted by CornedBee
cin won't ever have a eof condition unless the user figures out how to input an EOF character.
Any user worth anything should know how to signal EOF anyway.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Oct 1st, 2002, 07:26 AM
#34
Yeah, sure.
Ctrl+Z is the DOS way, but last time I tried it it didn't work...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 1st, 2002, 10:17 AM
#35
Thread Starter
Addicted Member
I dont see why someone cant get me started here with some of this code. I seriosuly just dont understand this language. I suck at all programming languages. I dont wanna fail the course. Im gonna look like a dope if I keep asking my teacher for help. I try and study this material, it just doesnt sink in. I cant grasp the dam concept.
-
Oct 1st, 2002, 08:54 PM
#36
Thread Starter
Addicted Member
how do u call a function ? isnt it function_name();
?
its not workin like that
Plus I cant get this to compile so far...
#include "stdafx.h"
#include <iostream>
#include <fstream>
int request();
using namespace std;
void expand(int&);
void request(int&, int&);
int main(int argc, char* argv[])
{
int *ptr;
ptr = new int[10];
int max_array = 10;
int count = 0;
request();
void expand(int &max_array)
{
int *temp;
temp = new int[2*max_array];
for(int i=0;i<max_array;i++)
temp[i] = ptr[i];
delete [] ptr;
ptr = temp;
max_array *= 2;
}
void request(int &count, int &max_array)
{
count = 0;
int integer;
while(true)
cout << "Enter An Integer: ";
cin >> integer;
for(integer<0)
return;
if(count == max_array)
expand()
ptr(count++) = integer
}
return 0;
}
the error msg's are C:\Program Files\Microsoft Visual Studio\assign3\assign3.cpp(37) : error C2601: 'expand' : local function definitions are illegal
C:\Program Files\Microsoft Visual Studio\assign3\assign3.cpp(48) : error C2601: 'request' : local function definitions are illegal
Error executing cl.exe.
assign3.obj - 2 error(s), 0 warning(s)
Last edited by chugger93; Oct 1st, 2002 at 09:06 PM.
-
Oct 2nd, 2002, 02:55 AM
#37
Fanatic Member
Sorry man, you are so lost, its not even funny!
Do it like this:
1. Call the input function
2. Call the menu function
3. Process the menu results
4. Repeat steps 2 and 3 untill exit
Here is a starting point,
PHP Code:
// the includes
#include <iostream>
using namespace std;
// Expand the array
void expand(int **ptr, int * max_array);
// reset the list data
void reset (int ** ptr, int * count, int * max_array);
// Get the input
void inputdata (int ** ptr, int * count, int * max_count);
int request ();
// Get the menu
int menu ();
// Get Min, Max, Mean, etc (you need to make these)
int GetMin (int * ptr, int count);
int GetMax (int * ptr, int count);
float GetMean (int * ptr, int count);
int main (void)
{
int * ptr = 0;
int count = 0;
int max_array = 10;
int temp = 0;
// create the initial list
ptr = new int [max_array];
// call the input numbers function
inputdata (&ptr, &count, &max_array);
// Now start the menu sequence
do {
temp = menu ();
swicth (temp)
{
case 0:
// exit
break;
// number 1 on the menu was pressed
case 1:
reset (&ptr, &count, &max_array);
inputdata (&ptr, &count, &max_array);
break;
// case 2, 3, 4, etc...
// default (anything not covered by case n)
default:
cout << temp << " is invalid!" << endl;
}
} while (temp != 0);
// the loop has been exited
return 0;
}
void expand(int **ptr, int * max_array)
{
int *temp;
int i;
temp = new int[2 * (*max_array)];
for(i = 0; i < *max_array; i++)
{
temp[i] = *(*ptr+i); //Changed
}
delete [] *ptr;
*ptr = temp;
max_array *= 2;
}
void reset (int ** ptr, int * count, int * max_count)
{
delete [] *ptr;
*max_count = 10;
*count = 0;
*ptr = new int [*max_count];
}
void inputdata (int ** ptr, int * count, int * max_count)
{
int temp = 0;
// start inputing numbers
do {
temp = request ();
// negative number exits the loop
if (temp < 0) {
break;
}
// expand the list if it gets too small
if (*count >= *max_count)
expand (ptr, max_count);
// now add this number to your list
(*ptr)[*count] = temp;
(*count)++;
} while (true);
}
int request ()
{
// Get an integer and return it
int integer;
cout << "Enter An Integer: ";
cin >> integer;
cin.ignore ();
return integer;
}
int menu ()
{
int item;
// Display menu and return input
cout << "===============================" << endl;
cout << " The Cool Menu " << endl << endl;
cout << " 0. Exit" << endl;
cout << " 1. Enter List" << endl;
// ... bla bla bla
cin >> item;
cin.ignore ();
// return item
return item;
}
// example of max function, use this to do the others (min, mean, bla)
int GetMin (int * ptr, int count)
{
int max = ptr[0];
for (int i = 0; i < count; i++)
{
if (max < ptr[i])
{
max = ptr[i];
}
}
return max;
}
There it is, a (almost complete) starting point, I hope now I shed light on you (cuz if i do anymore, i might as well turn it in with my name on it ).
Last edited by MoMad; Oct 2nd, 2002 at 03:00 AM.
-
Oct 2nd, 2002, 02:57 AM
#38
Fanatic Member
BTW: you cannot have ANY functions inside another function, main is a function, so thats why you got those errors.
-
Oct 2nd, 2002, 04:01 AM
#39
I suppose this was necessary. Thanks MoMad, else I would have got to do such a thing...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 2nd, 2002, 06:46 AM
#40
Thread Starter
Addicted Member
Momad: thanx!!
but the hell does this mean?
void expand(int **ptr, int * max_array)
{
int *temp;
int i;
temp = new int[2 * (*max_array)];
for(i = 0; i < *max_array; i++)
temp[i] = *(*ptr+i); //Changed
delete [] *ptr;
*ptr = temp;
max_array *= 2;
}
C:\Program Files\Microsoft Visual Studio\assign3\assign3.cpp(81) : error C2296: '*=' : illegal, left operand has type 'int *'
and it points to max_array **=2;
im gonna try this code, there are some differences, but im gonan try to see what u mean
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|