for loop ------ help

Started by
18 comments, last by ToohrVyk 18 years, 5 months ago
I have to write a program that reads a positive integer n from a file(.txt) , that integer is the number of the next integers , ( if the integer is 10 it is followed by 10 integers ). then the program has to read the next integers , finds the maximum of the first half of them , finds the minimum of the second half of them , finds the average of the even integers and find the sum of the odd integers. the integers in the file are showen like this 10 32 65 84 75 21 54 82 19 73 50 additionally , I don't know how to make it read them , should i make him read them by a simple cin or infile statments or by for loop . and how can I do the above operations . please help me .
Advertisement
What I would do is first read in the number N that has the value of number of integers.
With that number N, I would then allocate an array of N size.
Next read in all the N amount of integers and store them in the array.

Now that you have all the numbers in the array, you can close the file stream and just deal with the numbers in an array which would make the problem alot simpler.

Good Luck.
Quote:Original post by LostSource
What I would do is first read in the number N that has the value of number of integers.
With that number N, I would then allocate an array of N size.
Next read in all the N amount of integers and store them in the array.

Now that you have all the numbers in the array, you can close the file stream and just deal with the numbers in an array which would make the problem alot simpler.

Good Luck.

An array is not necessary. You can calculate everything you need to calculate in one loop with 4 variables. Inside the loop you'll need to check each number and see if it meets a specific requirement (is in the first or second half, is odd or even). If it does, then you update the variable that represents whatever it is that you're analyzing (maximum, minimum, average and sum).

HINT: you'll have to update the average variable the same as the sum, and calculate it after the loop has finished. The rest you can calculate on the fly.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Just calling cin>>someinteger enough times will work for this program.

Also this looks a lot like a homework problem (check this forums faq under Asking questions number 9). Basically we wont (or shouldn't) do your work for you, but if you show us what you've done so far we can help you get it working.
Smells like a homework problem to me!
Quote:Original post by stylin
Quote:Original post by LostSource
What I would do is first read in the number N that has the value of number of integers.
With that number N, I would then allocate an array of N size.
Next read in all the N amount of integers and store them in the array.

Now that you have all the numbers in the array, you can close the file stream and just deal with the numbers in an array which would make the problem alot simpler.

Good Luck.

An array is not necessary. You can calculate everything you need to calculate in one loop with 4 variables. Inside the loop you'll need to check each number and see if it meets a specific requirement (is in the first or second half, is odd or even). If it does, then you update the variable that represents whatever it is that you're analyzing (maximum, minimum, average and sum).

HINT: you'll have to update the average variable the same as the sum, and calculate it after the loop has finished. The rest you can calculate on the fly.



That is a much better solution.
What language are you using?

In C then an array is the way to go...but in C++ use a vector (std::vector)...make's it even easier.

Good luck..hope you get it sorted..:)
Gary.Goodbye, and thanks for all the fish.
Quote:Original post by garyfletcher
What language are you using?

In C then an array is the way to go...but in C++ use a vector (std::vector)...make's it even easier.

Good luck..hope you get it sorted..:)

How would using an array (C or C++ based) do anything but complicate the code?
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
I think the only way that it really complicates it, is that you just have to type more out. But also having all the values in a linear data stucture makes it easy to find the data you want. I have to agree with you though that just using a simple for loop reading in each integer and calculating the max of the first half, the min of the second and so on.... would work perfect. With the array, some people just feel more comfortable just using a for loop and traversing an array.

The time complexity of using the array way or just using a for loop. Depending on how you do it will equal O(N). So realy either way is good. It just depends if you like to type alot. = ]
Quote:Original post by LostSource
The time complexity of using the array way or just using a for loop. Depending on how you do it will equal O(N).

Let me ask you this, and maybe you'll see that total program time after iterating through an array rather than simply reading the file will be at least twice as long: How are you going to fill that array? [smile]
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid

This topic is closed to new replies.

Advertisement