Functions



Last edited on
So in your case would something like this be suitable?

If it's a vowel add it to vowelNo, if its something else then check it's not a space. If it's not a space add it to the consonantNo.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
using namespace std;

void vowelconsonant (string s)
{ 
	unsigned int stringLength = s.length();
	int vowelNo = 0;
	int consonantNo = 0;

	for (unsigned int i = 0; i < stringLength; i++)
	{
		if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
		{
			vowelNo += 1;
		}
		else
		{
			if(s[i] != ' ')
			{
				consonantNo += 1;
			}
		}
	}
	cout << s << endl;
	cout << "Vowels:    " << vowelNo << endl;
	cout << "Consonant: " << consonantNo << endl;
}

int main()
{
	vowelconsonant("abcdefg hijklmnop qrs tuv wxyz");
	return 0;
}
Vowel-Consonant Problem

Hey, Final12 you were not using 'if' statement correctly. Also review the following code.
Let me know if you find any problem.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// TestAppln.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

// Have to declare the function since it's defined after main function.
void vowelconsonant(string s);

// Win32 console application has _tmain as the starting function.
int _tmain(int argc, _TCHAR* argv[])
{

cout<<"Enter a string :";
string s;

// getline is to be used since cin doesn't read once it encounters space
// or end of line. Hence cin won't work if you are giving more than one word.
getline(cin,s);

// function called
vowelconsonant(s);
return 0;
}

void vowelconsonant(string s)
{
int vowelno = 0;
int space = 0;
int consonantno= 0;

int str = s.length();
cout<<"Lenght of the string equals : "<<str;

for (int i=str-1; i >=0; i--)
{
// checks each character of string, In if condition "==" is used and not "=" for comparison.
if ((s.at(i) == 'a')||(s.at(i) == 'e')||(s.at(i) == 'i' )||(s.at(i) == 'o')||(s.at(i) == 'u'))
vowelno = vowelno + 1;
else if(s.at(i) == ' ')
space = space + 1;
else
consonantno = consonantno + 1;
}

cout << "\n Vowel: " << vowelno;
cout << "\n Space: " << space;
cout << "\n Consonant:" << consonantno;

// I am using cin just because user can see the output till he doesn't press enter.
cin>>s;
}
Thanks alot for your help guys, really appreciate it =)

I used parts of code from both of your examples, I always forget that '==' is equality in If statements haha.
No problem.
functions part 2

basically I want to get a certain value down to 1

so if i get value say.. 500 I want to get it down to 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int goto1(int n)
{    
    int steps = 0;
    while (n > 1)
    {
      if ( n%2 == 0)
         n = sqrt(n);
      else 
         n = n*2;

    steps += 1
    }
    cout <<right<< "Number of steps: " << steps << endl; 
}


it says that theres an error in the n = sqrt(n) line but i've tried converting it to a double by using static cast and it doesn't seem to work

basically it square roots the answer until it reaches 1
Last edited on
That probably won't bring the number down to 1 anyway - the square root of 500 is 22.xxxxxx, and that doesn't fulfill if(n % 2 == 0), and it'll just doubling itself.

If you want to deal with decimal numbers with an int argument, you could create a double in the function and copy the int argument into the double and work using that double to make it easier.
how do I create a double inside a function? not sure about this
Square Root - Modulous problem


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// sqrt.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<math.h>

using namespace std;

int goto1(double n);

int _tmain(int argc, _TCHAR* argv[])
{
	double n;
	cout<<"Enter any integer :";
	cin>>n;
	goto1(n);

	return 0;
}

int goto1(double n)
{    
    int steps = 0;
    
       // I have also declared n<1000 condition to have a upperbound
	while (n > 1 && n < 1000)
	{

          // modulus function requires the argument to be integer
          // so we can go the other way to implement it
          // modulus is based on the below concept, and so we are
          // implementing modulus and can also have double as the operand.
          // not to forget use of double is required in sqrt argument.

          if ( (n - ((int)(n/2))*2) == 0)
	     {
		  n = sqrt(n);
	          cout<<"\n"<<n;
	     }
          else 
	     {
		  n = n*2;
		  cout<<"\n"<<n;
	     }

          steps += 1;
         }

    cout <<right<< "\n Number of steps: " << steps << endl; 
    cin>>n;

    return 0;
}
Topic archived. No new replies allowed.