Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 04-10-2007, 01:22 PM   #1
Member (10 bit)
 
faint545's Avatar
 
Join Date: Mar 2006
Location: Maryland
Posts: 550
Exclamation C++ Help

Right now i'm enrolled in a course introducing C++. Before I enrolled, I had alot of experience with C++ so the course is quite easy for me but I've now hit a brick wall in this project i'm currently writing.

For this project, we have to...
Quote:
Write a program that processes integers read from an input file. A sentinel of 0 will mark the end of the integers. Output should be sent to the screen. The file should be passes as a reference parameter to the readEchoStore function.
Also, when we read the numbers from the file, we store the numbers into an array. I've gotten to the part where you process the sentinel but here's where i'm stuck. We are also to check to see if the amount of numbers in the file exceed the 15 integer limit. Meaning, we can not allow any more than 15 integers into the array. I really have no idea on how to code this part...

Below is the link to the code that I have written up so far..
project7.pdf
__________________
Kerberos2:
Corsair Obsidian 700D
ASUS M4A89TD
Radeon HD 5850
A-DATA 8GB DDR3 1333
AMD Phenom II x6 1055T @ 2.8GHz w/ Corsair H50
Corsair 850HX PSU
WD Blue 160GB 7200 RPM SATA HDD
WD Black 640GB 7200 RPM SATA HDD
ASUS DVD/CD Drive
faint545 is offline   Reply With Quote
Old 04-10-2007, 02:30 PM   #2
FLG
Member (11 bit)
 
FLG's Avatar
 
Join Date: May 2003
Location: Brooklyn, NY
Posts: 1,798
Send a message via AIM to FLG
Why cant you create a second array to hold ALL the numbers which are in the file. Then take the first 15 while also checking if any one of em is the sentinel.
FLG is offline   Reply With Quote
Old 04-10-2007, 03:19 PM   #3
Member (9 bit)
 
Join Date: Feb 2005
Posts: 392
ok,
to limit the number of inputs to 15 integers just change the while loop's condition a bit:

while((temp_store != 0) && (count <=15))
- ==============>>just add this part


and to count the numbers of INTEGERs exceeding 15, do:

while(!eof)
read and count each;
after you done with the first 15 numbers.

***************************


however, the way I understand the Project's statement is that:

readEchoStore is a FUNCTION, NOT an array.

should be declare as:

void readEchoStore(ifstream &);

the input file name is suppose to be passed as reference to it, like this:

readEchoStore(inName);

also "inName" the input filename shouldnot be fixed, should be declare as:

char *inName;

instead.
__________________
words to live by:
others don't know, I know.
others know, I know more.
others know more, I excel.
one shouldnt read this far; above, is meant as an encouragement, translated from a Chinese Proverb.
"He who angers you conquers you." : Elizabeth Kenny

Last edited by alfie2; 04-10-2007 at 03:22 PM.
alfie2 is offline   Reply With Quote
Old 04-10-2007, 05:07 PM   #4
Moderator
Staff
Premium Member
 
Join Date: Aug 2003
Location: Richmond, VA
Posts: 7,835
You can create an if/else structure inside the while loop. The while-loop will execute until the '0' sentinel arrives (alternatively, and probably more efficiently, you can use a do-while loop structure instead). Create a new boolean variable that will essentially act as an error-check statement. Place the if structure on the count++, where it will run until count==15. Label the boolean true. Else, label the boolean false. Make the true/false boolean as part of the while/do-while loop condition.

kram
__________________
"For today, goodbye. For tomorrow, good luck. And forever, Go Blue!"
University of Michigan President Mary Sue Coleman

Last edited by kram 2.0; 04-10-2007 at 09:02 PM.
kram 2.0 is offline   Reply With Quote
Old 04-13-2007, 10:08 AM   #5
Member (10 bit)
 
faint545's Avatar
 
Join Date: Mar 2006
Location: Maryland
Posts: 550
I got as far as creating a reference function and putting all the processing in there but now i'm required to create two more functions. I have a new updated file of my source code below.
Project7.pdf
What i'm to do is create two more functions that will calculate the sum and calculate the min & max. How do I go about creating those fucntions and passing the variable needed from function readEchoStore to the other two functions? Would I use a reference function?
faint545 is offline   Reply With Quote
Old 04-14-2007, 07:42 AM   #6
Member (9 bit)
 
Join Date: Feb 2005
Posts: 392
you are still using "readEchoStore" as an array, no no no,
you should have array with another name like "storagearray" or whichever
you wish, just dont use "readEchoStore" as an array name.

here are the two functions:

void arrayMaxMin(const int iarray[], int &imax, int &imin)
{
int i;

//find maxium value in array:
imax = iarray[0];
for(i=1;i<=14;i++)
{
if(iarray[i]>imax) imax=iarray[i];
}

// find minium value in array:
imin = iarray[0];
for(i=1;i<=14;i++)
{
if(iarray[i]>imin) imin=iarray[i];
}

}// arrayMaxMin

int arraySum(const int iarray[])
{
int i;
int total=0;
for(i=0;i<=14;i++) total = total +iarray[i];
return total;
}//arraySum


=========================================

now you can use these two functions inside "readEchoStore" function:


void readEchoStore(infile)
{
int storagearray[VARIABLE];
int highest,lowest;

:
:
:

arrayMaxMin(storagearray, highest,lowest);
sum = arraySum(storagearray);

cout << endl;
cout << "\nThe number of integers entered was " << count < cout << "The sum is " << sum << endl;
cout << "The maximum integer is " << highest << endl;
cout << "The minimum integer is " << lowest << endl;
}
alfie2 is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:27 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2