# for loop ------ help

## Recommended Posts

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 .

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by LostSourceWhat 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.

##### Share on other sites
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.

##### Share on other sites
Smells like a homework problem to me!

##### Share on other sites
Quote:
Original post by stylin
Quote:
 Original post by LostSourceWhat 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.

##### Share on other sites
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..:)

##### Share on other sites
Quote:
 Original post by garyfletcherWhat 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?

##### Share on other sites
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. = ]

##### Share on other sites
Quote:
 Original post by LostSourceThe 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]

##### Share on other sites
Well if you use a for loop to store an array that is O(N) and then if you use another for loop to loop through the array its another O(N) time. So O(N) + O(N) is O(2N). Now if you say this:

C = N

C' = 2 * N

C' is just an arbitrary constant of C. So from here you can say that its just O(C) where C is just a constant amount. So its still O(N).

##### Share on other sites
Quote:
 Original post by LostSourceC' is just an arbitrary constant of C. So from here you can say that its just O(C) where C is just a constant amount. So its still O(N).

I'll take your word for it; I'm sure the time does increase at the same rate for any N. My point was using an array introduces unnecessary logic that only works to make the end-result less efficient and slow - instead of reading numbers, think about reading classes with expensive constructors/destructors/copies and assignments.

##### Share on other sites
An interesting variation on this theme is:

You are given a list of integers, under the form of a function which returns the next integer when called, and a function which returns true if and only if there is a next integer. Can you compute the maximum element of the second half of the list in constant space?

##### Share on other sites
Quote:
Original post by stylin
Quote:
 Original post by LostSourceC' is just an arbitrary constant of C. So from here you can say that its just O(C) where C is just a constant amount. So its still O(N).

I'll take your word for it; I'm sure the time does increase at the same rate for any N. My point was using an array introduces unnecessary logic that only works to make the end-result less efficient and slow - instead of reading numbers, think about reading classes with expensive constructors/destructors/copies and assignments.

stylin: Look up "big Oh" on time complexity of problems and algorthims. O(N) is linear complexity (for loop) and is always linear. Even if you do 1 for loop or 10 for loops (not nested for loops) its still going to be O(N) program/fuction. But if for some reason it took a nested for loop to do this program it would be O(N^2), but I don't see how anyone could make this program that complex. = ]

ToohrVyk: do you mean constant space? or constant time?

##### Share on other sites
Quote:
 Original post by LostSourcestylin: Look up "big Oh" on time complexity of problems and algorthims. O(N) is linear complexity (for loop) and is always linear. Even if you do 1 for loop or 10 for loops (not nested for loops) its still going to be O(N) program/fuction.

Look, I think you're missing the point. I'm not talking about complexity. Yes, a for loop is a for loop is a for loop. But you don't seem to understand that adding more for loops will take longer for the program to complete.

Let me illustrate: If started at the same time, which of these two functions is going to finish before the other one?
namespace light {   void waste_time() {      for( int i=0; i<1000000; ++i ) ;   }}namespace heavy {   void waste_time() {      for( int i=0; i<1000000; ++i ) ;      for( int i=0; i<1000000; ++i ) ;      for( int i=0; i<1000000; ++i ) ;      for( int i=0; i<1000000; ++i ) ;      for( int i=0; i<1000000; ++i ) ;      for( int i=0; i<1000000; ++i ) ;      for( int i=0; i<1000000; ++i ) ;      for( int i=0; i<1000000; ++i ) ;      for( int i=0; i<1000000; ++i ) ;      for( int i=0; i<1000000; ++i ) ;   }}

##### Share on other sites
thank you all for your help .

typing is not hte problem , but submitting the assignment at time is the difficult mission ,

I have worked on this homework fo about 5 hours only to produce this

{ifstream infile;infile.open("number.txt");infile>>nb;infile>>x;//finding the maximum of the first half  max=x; // assuming that the first number is the maximumfor ( int i=1 ; i < ((nb/2)) ; i++ ){	infile>>next;  if ( next > max  )       max=next;    //changing the maximum if the next number is higher.}cout <<"The maximum of the first half is: "<<max<<endl;//finding the minimum of the second half. infile>>y;min=y;for ( int j=((nb/2)+1) ; j < nb ; j++ ){	 	 	   infile>>next2;	if ( next2 < min )		min=next2;}cout<<"The minimum of the second half is: "<<min<<endl;infile.close();}{ifstream infile;infile.open("number.txt");//finding the sum of all even numbersinfile>>nb;infile>>number;   if ( number%2==0)        sum=number;   else sum=0;for ( int k=2 ; k<=nb ; k++ ){ 	     infile>>num; if ( num%2==0 )     sum=sum+num; }cout<<"The sum of all even numbers is: "<<sum<<endl;infile.close();}//finding the average of all odd  numbers{ifstream infile;infile.open("number.txt");infile>>nb;infile>>number2;counter=0;//to calculate the number of odd numbers that will be devided by thier sum.      if ( number2%2!=0){        sum2=number2;        counter=1;}  else sum2=0;for ( int f=2 ; f<=nb ; f++ ){ 	   infile>>num2;if ( num2%2!=0 ){sum2=sum2+num2;counter++;}} average=sum2/counter;cout<<fixed<<showpoint<<setprecision(2);cout<<"The average of all odd numbers is: "<<average<<endl;infile.close();}	return 0;}

##### Share on other sites
I am sorry but I didn't know that I can't ask for help related to any homework.

##### Share on other sites
Quote:
 Original post by LostSourceToohrVyk: do you mean constant space? or constant time?

Quote:
 Original post by yours trulyCan you compute the maximum element of the second half of the list in constant space?

##### Share on other sites
Quote:
 Original post by ToohrVykAn interesting variation on this theme is:You are given a list of integers, under the form of a function which returns the next integer when called, and a function which returns true if and only if there is a next integer. Can you compute the maximum element of the second half of the list in constant space?

I'm going to assume that it's possible, since you asked, but I have no idea how, short of storing every number in an array as you read them. Since that's linear space, I'm stumped. What's the solution?

##### Share on other sites
Quote:
Original post by stylin
Quote:
 Original post by ToohrVykAn interesting variation on this theme is:You are given a list of integers, under the form of a function which returns the next integer when called, and a function which returns true if and only if there is a next integer. Can you compute the maximum element of the second half of the list in constant space?

I'm going to assume that it's possible, since you asked, but I have no idea how, short of storing every number in an array as you read them. Since that's linear space, I'm stumped. What's the solution?

It's impossible. But proving that you can't is hard (and along the lines of the proof that comparison-based sorts cannot perform faster than n log n).

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
628345
• Total Posts
2982191

• 9
• 24
• 10
• 9
• 13