{ Help in looping , plz help ! }

Help in 2 looping problems

Page 1 of 1

13 Replies - 1052 Views - Last Post: 02 May 2008 - 10:22 AM Rate Topic: -----

#1 Mi$s-Q8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-April 08

{ Help in looping , plz help ! }

Posted 30 April 2008 - 08:20 AM

hey every one ..

This is my first post in this forum, and i hope to find help !!

i have an exam 2morrow .. in C++, and i'm still having problems in understanding looping .. THIS forum is realy my last hope :blink:

I've got 2 problems in looping, i realy wish help in solving it and understanding the solution ..



1) write a program to count the vowels and alphabets in an input line of characters. Read the characters one at a time until you encounter the end-of-line character. Then print out :
- the number of occurences of each of the vowels ( a, e, i, o, u ) whether capital or small.

- the total number of alphabets, and

- the integer percentage of each vowel.

Note that the program should ignore all non-alphabrts.

Example;
for the following input :
A good word is a charity

suggested output format is;

number of vowels:
a 3 , e 0 , i 2 , o 3 , u 0

total number of letters is 19

Integer Percentages of each vowel:
a 16% , e 0% , i 11% , o 16% , u 0%


--------------------------------------------------------

2) Tragectory of a stone Consider the trajectory of a stone that is hurled into the air with an initial velocity v at an angle to the horizontal of ( @ ) radians. Neglecting drag due to friction with the atmoshere, the following equations describe the stone's distance ( d ) from the initial spot and the height (h) of a stone at time t :
d = v.t.cos@ ... as for distance
h = v.t.sin@-1/2 g.t^2 ... as for height

where g is the acceleration due to gravity ( 9.8m/s2 (
or 32 ft/s^2

a) write a program that will generate a table that gives the distance and hieght of the stone for every tenth of a seconds, assuming that the initial velocity is in ft/s and the angle in degrees are read from the keyboard.

B) write program that will generate a table that gives the distance and height of the stone for every tenth of a second until it hits of a second until it hits the ground. Assume that the initial velocity is 75 ft/s at an angle of 30[color=#3366FF]


** @ is the angle theta.
** ^2 , is power 2 or square !!

This post has been edited by Mi$s-Q8: 30 April 2008 - 08:24 AM


Is This A Good Question/Topic? 0
  • +

Replies To: { Help in looping , plz help ! }

#2 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: { Help in looping , plz help ! }

Posted 30 April 2008 - 08:43 AM

Okay - and what's the problem?

Do you not understand looping? Try taking a look at a C++ looping tutorial.

If you have some code that you're having trouble with, post it - we'll take a look and try to help you out.
Was This Post Helpful? 0
  • +
  • -

#3 Sepanto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 20-March 08

Re: { Help in looping , plz help ! }

Posted 30 April 2008 - 08:56 AM

Here's the first one - I wrote it for a class once so i don't mind posting it seeing that I don't need to redo the work :
#include <stdio.h>
int main()
{
char sentence;
/*ca=counter of a's, ce counter of e's... cl counter of letters, cno of non 
letters, cto of total. aca=acountera/cto...
*/
int ca=0, ce=0,ci=0,co=0,cu=0,cl=0;
int cno=0,cto=0;
//aca - rellative part of a's, ece - relative part of e's.... nocono relative 
//part of non
float ece,aca,ici,oco,ucu,lecl,nocno;
	 scanf("%c",&sentence);
	 while (sentence!='\n')
	{
	//switch-case loop on the sentence.
   switch(sentence)
	{
			//if i got a or A i add 1 to 1 counter, and 1 to total coutner and 
			//get next char.
			case 'a':
			case 'A':
			 ca++;
			 cto++;
			  scanf("%c",&sentence);
			break;
			//if i got an E or an e i add 1 to e counter and total and 
			//total counter and get next char.
			case 'e': 
			case 'E':
			  ce++;
			  cto++;
			   scanf("%c",&sentence);
			break;
			//if i got an I or an i i add 1 to u counter and 
			// and total counter and get next char.
			case 'i': 
			case 'I':
			   ci++;
			   cto++;
				scanf("%c",&sentence);
			break;
			// if i got an o or an O i add one to o counter and total and 
			//get next char.
			case 'o':
			case 'O':
			   co++;
			   cto++;
				scanf("%c",&sentence);
			break;
			//i i got a u or a U i add one to u counter and total and get next char.
			case 'u':
			case 'U':cu++; 
					 cto++;
				scanf("%c",&sentence);
			break;
			default:
			//if i get a letter i add 1 to letter counter and get next char.
			if (((sentence>='a') && (sentence<='z')) || ((sentence>='A') && (sentence<='Z')))
				 {
				   cl++;
				   cto++;
				   scanf ("%c",&sentence);
				 }
				 //if i did not get a letter i add 1 to non-letter counter
				 //and 1 to total. 
			else if (sentence!='\n')
				{
				  cno++;
				  cto++;
					 scanf ("%c",&sentence);
				}				 
				  lecl=(float)cl/cto;
			  lecl=lecl*100;
				  aca=(float)ca/cto;
				  aca=aca*100;
				  ece=(float)ce/cto;
			  ece=ece*100;
				  ici=(float)ci/cto;
			  ici=ici*100;
				  oco=(float)co/cto;
			  oco=oco*100;
				  ucu=(float)cu/cto;
				  ucu=ucu*100;
				  nocno=(float)cno/cto;
			  nocno=nocno*100;
			  break;
			  scanf ("%c", &sentence);
	}
	}
					printf ("Numbers of characters:\n");
					printf("a %d; e %d; i %d; o %d; u %d;",ca,ce,ci,co,cu);
					printf("letters %d; other %d\n ",cl,cno);
					printf("Percentages of total:\n ");
					printf("a %.1f%%; e %.1f%%;i %.1f%%  ",aca,ece,ici);
					printf("; o %.1f%%; u %.1f%%;",oco,ucu);
					printf("letters %.1f%%;",lecl);
					printf("other %.1f%% \n",nocno); 
					return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 Mi$s-Q8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-April 08

Re: { Help in looping , plz help ! }

Posted 30 April 2008 - 09:04 AM

{ girasquid }

i've already read my text book and understand the rules of the while and for statements , but i dot know how to get start in writing a program using loops !!!

i'll try taking tutorials .. thanx;
Was This Post Helpful? 0
  • +
  • -

#5 Mi$s-Q8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-April 08

Re: { Help in looping , plz help ! }

Posted 30 April 2008 - 09:13 AM

{ Sepanto }

thanx alot .. i'll try to understand the solution
and turn it into " #include <iostream> "

thanx again !
Was This Post Helpful? 0
  • +
  • -

#6 jeronimo0d0a  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 146
  • Joined: 03-March 08

Re: { Help in looping , plz help ! }

Posted 30 April 2008 - 09:31 AM

If you think about every single step the machine must take, it may help. It would go on forever if you don't tell it when to stop. Here's a very small loop. :)

bool flying = true ; // when you throw it, it is flying
float horizontal=0, vertical = 0 ;
// throw the rock
// calculate velocities in the horizontal and vertical. basic trig
while( flying ) // do this while flying is true
{
// add negative acceleration of gravity to vertical
// air resistance ???
// calculate new position
// key here, if underground, not flying, loop ends
// tell the program so
if ( vertical <= 0 ) { flying=false ; }
}
Was This Post Helpful? 0
  • +
  • -

#7 Mi$s-Q8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-April 08

Re: { Help in looping , plz help ! }

Posted 30 April 2008 - 10:02 AM

{ girasquid ..}

i've try as U said & this is my code
there is an error i couldn't discover it; all percentages are = 0 ??!!
con you tell me the reason, plz ?

#include <iostream> 

using namespace std;

int main()
{
	int dummy;
	char c;
	int countA=0, countE=0, countI=0, countO=0, countU=0, totalCount=0;
	
	cout << "Enter a line : ";
	cin.get(c);
	
	while ( c!='\n' )
	{
		  switch (c)
		  {
				 case'a':
				 case'A': countA++;
						  break;
				 case'e':
				 case'E': countE++;
						  break;  
				 case'i':
				 case'I': countI++;
						  break;
				 case'o':
				 case'O': countO++;
						  break;
				 case'u':
				 case'U': countU++;
						  break;
		   }
		   totalCount++;
		   
		   if ( c>='A' && c>='Z' || c>='a' && c>='z' )
		   totalCount++;
		   
		   cin.get(c);
		   
	  }
	  
	  cout << "Number of vowels : " << endl;
	  cout << "a " << countA << "; e " << countE << "; i  " << countI;
	  cout << "; o " << countO << "; u " << countU << endl; 
	  cout << endl;
	  
	  cout << "Total number of letters is " << totalCount << endl;
	  cout << endl;
	  
	  cout << "Integer Percentages of each vowel :" << endl;
	  cout << "a " << (countA/totalCount)*100 << "%";
	  cout << "; e " << (countE/totalCount)*100 << "%";
	  cout << "; i " << (countI/totalCount)*100 << "%";
	  cout << "; o " << (countO/totalCount)*100 << "%";
	  cout << "; u " << (countU/totalCount)*100 << "%";
	  
	  cin >> dummy;
	  return 0;
	  
	  }
	


Was This Post Helpful? 0
  • +
  • -

#8 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: { Help in looping , plz help ! }

Posted 30 April 2008 - 12:30 PM

Your problem is where you try to calculate the percentages. It should look like this:
      cout << "Integer Percentages of each vowel :" << endl;
      cout << "a " << (100/totalCount)*countA << "%";
      cout << "; e " << (100/totalCount)*countE << "%";
      cout << "; i " << (100/totalCount)*countI << "%";
      cout << "; o " << (100/totalCount)*countO << "%";
      cout << "; u " << (100/totalCount)*countU << "%";
Basically, 100/totalCount will give you the % value of 1 character. Then you want to multiply this by the number of a certain vowel to get your percentage.

How are you getting on with loops now? Are you more confident, or do you need some more help?

Hope this helps :)

This post has been edited by gabehabe: 30 April 2008 - 12:31 PM

Was This Post Helpful? 0
  • +
  • -

#9 jeronimo0d0a  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 146
  • Joined: 03-March 08

Re: { Help in looping , plz help ! }

Posted 30 April 2008 - 12:46 PM

Of course alll the percentages are zero. here's why. analysis of
(countU/totalCount)*100
when countU = 25 and totalCount = 50
50% assigned to an integer = 0 since integers don't do fractions
* 100 is still zero

(countU * 100) = 2500
/ totalCount = 50 since integers can hold 50
Was This Post Helpful? 0
  • +
  • -

#10 Mi$s-Q8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-April 08

Re: { Help in looping , plz help ! }

Posted 30 April 2008 - 01:47 PM

{ gabehabe..}
{ jeronimo0d0a..}

Oh god finally it works :D
thank U .. thank U .. thank U

problem 1 .. is done!

#include <iostream> 

using namespace std;

int main()
{
	int dummy;
	char c;
	int countA=0, countE=0, countI=0, countO=0, countU=0, totalCount=0;
	
	cout << "Enter a line : ";
	cin.get(c);
	
	while ( c!='\n' )
	{
		  switch (c)
		  {
				 case'a':
				 case'A': countA++;
						  break;
				 case'e':
				 case'E': countE++;
						  break;  
				 case'i':
				 case'I': countI++;
						  break;
				 case'o':
				 case'O': countO++;
						  break;
				 case'u':
				 case'U': countU++;
						  break;
		   }
					 
		   if ( c>='A' && c>='Z' || c>='a' && c>='z' )
		   totalCount++;
		   
		   cin.get(c);
		   
	  }
	  
	  cout << "Number of vowels : " << endl;
	  cout << "a " << countA << "; e " << countE << "; i  " << countI;
	  cout << "; o " << countO << "; u " << countU << endl; 
	  cout << endl;
	  
	  cout << "Total number of letters is " << totalCount << endl;
	  cout << endl;
	  
		   
	  cout << "Integer Percentages of each vowel :" << endl;
	  cout << "a " << float(countA*100)/totalCount  << "%";
	  cout << "; e " << float(countE*100)/totalCount << "%";
	  cout << "; i " << float(countI*100)/totalCount << "%";
	  cout << "; o " << float(countO*100)/totalCount << "%";
	  cout << "; u " << float(countU*100)/totalCount << "%";
	  
	  cin >> dummy;
	  return 0;
	  
	  }
	



can any body give me a small hint for the 2nd problem :PpP~

thank u .. all;
Was This Post Helpful? 0
  • +
  • -

#11 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: { Help in looping , plz help ! }

Posted 30 April 2008 - 01:55 PM

What is your 2nd problem?
Was This Post Helpful? 0
  • +
  • -

#12 Mi$s-Q8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-April 08

Re: { Help in looping , plz help ! }

Posted 30 April 2008 - 02:04 PM

it's already in my basic post ..

2) Tragectory of a stone Consider the trajectory of a stone that is hurled into the air with an initial velocity v at an angle to the horizontal of ( @ ) radians. Neglecting drag due to friction with the atmoshere, the following equations describe the stone's distance ( d ) from the initial spot and the height (h) of a stone at time t :
d = v.t.cos@ ... as for distance
h = v.t.sin@-1/2 g.t^2 ... as for height

where g is the acceleration due to gravity ( 9.8m/s^2 )
or 32 ft/s^2

a) write a program that will generate a table that gives the distance and hieght of the stone for every tenth of a seconds, assuming that the initial velocity is in ft/s and the angle in degrees are read from the keyboard.

B) write program that will generate a table that gives the distance and height of the stone for every tenth of a second until it hits of a second until it hits the ground. Assume that the initial velocity is 75 ft/s at an angle of 30


** @ is the angle theta.
** ^2 , is power 2 or square !!


i just need a hint !
* how can i generate a table ?
* in part b , what is the time ' t ' i should use ?
* what will the loop look like , i do have problems in getting started with any problem :S ..
Was This Post Helpful? 0
  • +
  • -

#13 Sepanto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 20-March 08

Re: { Help in looping , plz help ! }

Posted 01 May 2008 - 04:48 AM

How was the exam?
Was This Post Helpful? 0
  • +
  • -

#14 Mi$s-Q8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-April 08

Re: { Help in looping , plz help ! }

Posted 02 May 2008 - 10:22 AM

it will not gnna be full mark .. but it will be fine
the best part .. i started to understand loopinh ..

thanks 4 asking :)

This post has been edited by Mi$s-Q8: 02 May 2008 - 10:26 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1