insamd 0 Newbie Poster

G'Day guys,

Being working on some code, and was just wondering if there are any obvious places i have gone wrong. I am trying to make 2 functions, 1 that will find the factors of a given number, and the other function to find the percentage of the even factors.
E.g. 12: factors = 1,2,3,4,6,12 even factors = 2,4,6,12
so the percentage of even factors is = (4/6)*100
any help is always appreciated.

to find the factors:

int factors (int number)
{
    for(int i=1; i<=number; i++)
      {
         if(number%i==0) 
         {
            if (i%2==0) 
            {
              return i;
            }
         }
      }
}

and.. to find the percentage of even factors

int percentageOfEvenFactors (int number)
{
    int percentage=0;
    int allFactors;
    int evenFactors=0;
    for (int i=1; i<=number; i++)
    {
        if (number%i==0)
        {
           i=allFactors;
                        
          if(i%2==0)
          {
            evenFactors++;
          }
          percentage=(allFactors/evenFactors)*100;
        }
    }
    cout<<percentage<<endl;
}

Cheers.

insamd 0 Newbie Poster

ahhh how silly of me!! thanks, i managed to get it working! just fixed up my code a little and voila im seeing the results.
thanks for your help, appreciated!!! incrementing your rep :p

insamd 0 Newbie Poster

thanks for your prompt reply. i called initialized letterCount and the letters come up, except with 0 next to each one, so this i gather is where i need to actually get the characterCount happening so. I was thinking that maybe

while (!inStream.eof())
    {
          characterCount(ch, letterCount);
    }

will work... but it doesn't show up anything fullstop. This might be where the eof is messing me up.
I 'can' use eof though can't i, its just im using the incorrect code!?
thanks again

insamd 0 Newbie Poster

hey guys,
i am trying to get my code to count the number of characters in a file. However, the program is continuously going through the characters (so it goes from a-z thousands of times) when i want it to say how many times 'a' occured and 'b' and so on, in the text file.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void characterCount(char ch, int list[]);
void writeTotal(int list[]);
void initialize(int list[]);
int main()
{
    int letterCount[26];
 string fileName;
 ifstream inStream;
 inStream.open("C:\\data.txt");
 if (inStream.fail()) 
    {
  cout << "could not open input file";
  return 1;
    }
    
    while (!inStream.eof())
    {
         writeTotal(letterCount);
    }
  system("PAUSE");
  return 0;
}
void initialize(int list[])
{
     
     for(int j=0; j<26; j++)
             list[j]=0;
}
void characterCount(char ch, int list[])
{
     int index;
     ch=tolower(ch);
     
     index=static_cast<int>(ch)-static_cast<int>('a');
     
     if (0 <= index & index <26)
           list[index]++;
}
void writeTotal(int list[])
{
     int index;
     
     for(index=0; index<26; index++)
                  cout<<static_cast<char>(index
                                             +static_cast<int>('a'))
                  <<"("<<list[index]<<")"<<endl;
}

When i change my while loop to:

while(!inStream.eof())
      {
         inStream>>fileName;
         cout<<fileName<<" ("<<fileName.size()<<")"<<endl;
      }

i manage to get all the words to display with the amount of characters in each word. I am struggling to make the transition from calculating how many in a word to how many of each letter.
Any help will be appreciated.

insamd 0 Newbie Poster

Thanks heaps. Sorry for putting you through that haha.

Thankyou to everyone else's input too, extremely helpful.

insamd 0 Newbie Poster

hey thanks for your ongoing help andor, appreciate it.

My only concern is that, no matter what string i put in, it outputs 4006960. I am getting closer, but not quite there...
Maybe it is not being split up correctly.
If i put in john i should get a value of: 4635
j has 4 1's. o has 6 1's. h has 3 1's. n has 5 1's.

I tried a few changes with the code you gave me, to see if the 4006960 would come up again

char tmp[2] = { 0, 0 };
      tmp[0] = name[i];
      answer += atoi(tmp);

i still had the same output of 4006960, so i get the feeling i am adding all the binary's together. So, 1101010 + 1101111 + 1101000 + 1101110 = 4006960. I believe the program isn't finding the number of 1's in the first binary and then adding that to the number of 1's found in the second binary. I might not have explained it well. With the above output, i should get: 4635.

Sorry for being so unclear with what i was trying to do. Thanks again for all your help, appreciate it.

insamd 0 Newbie Poster

cheers for them, ill take a look over it.

here is an update on my array and how to add them together, although it doesn't seem to add them for me... i have kept the code to output the binary (to make sure it is still doing it) but when i try add together i always get 0...

for (unsigned int i=0; i<name.size(); i++)
    {
      cout << name[i] << " binary ("; 
      decToBin(name[i], 2);
      cout << ")" << endl;
 
      char tmp[2] = { 0, 0 };
      tmp[0] = name[i];
      answer = atoi(tmp);
 
      if (name[i]=='1')
      {
      count++;
      }
 
    }
   cout<<answer<<endl;
   system("PAUSE");
   return 0;

So my output is:
j binary (binary #)
o binary (binary #)
h binary (binary #)
n binary (binary #)
0

i also tried to use

if (answer[i]=='1')
count++;
insamd 0 Newbie Poster

thanks for your help guys appreciate it...

i am struggling to grasp the concept! but not to worry, as i was going to ask if anyone knew of some good tutorials or books that i can get to gain a better understanding.. i just can't comprehend the logic to what i need to do. So i figure i need to go back to step 1.

Thanks again guys.

insamd 0 Newbie Poster

sorry about my explanation... i don't wish to change into decimal, i was wanting to get the output of the binary's and add each binary's 1's...
1101110 number of 1's = 5
1001101 number of 1's = 4
1011011 number of 1's = 5

Then join those numbers together, so the number would be 545.
Sorry for the misunderstanding, my bad.

insamd 0 Newbie Poster

i was trying to make it into an integer, because when i try to just div 10 the decToBin(name,2) it won't let me... thats my understanding of what i had to do. But it didn't work. So yeah thats why i am asking here, sorry limited knowledge :(

insamd 0 Newbie Poster

Hey guys,

I was wondering if anyone can help me in terms of using atoi to get my my results to add the 1's of my binary output and then join them together... eg. 110101 and 100101 is 4 and 3 = 43.
I was thinking of integer dividing the number by 10, but i am unsure of how to do that.
Here is my code:

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void decToBin(int number, int base);
int main()
{
   int answer;
   int count=0; 
   cout<<"Enter a string: ";
   string name;
   getline(cin,name);
 
   /* for (unsigned int i=0; i<name.size(); i++)
    {
      cout << name[i] << " binary (";
      decToBin(name[i], 2);
      cout << ")" << endl; 
    } 
    */    
    for (unsigned int i=0; i<name.size(); i++)
    {
        decToBin(name[i],2)=answer;
        answer=answer/10;    
        cout<<answer<<endl;
    }
   system("PAUSE");
   return 0;
}    
void decToBin (int number, int base)
{
     if (number>0)
     {
        decToBin(number/base, base);
        cout << number%base;
     }
}

The commented part gets the numbers into binary, the other loop under it i was trying to do so decToBin became an int and could then div 10.
As you can tell i am not sure of the direction i should go, any help would be appreciated, thanks!

insamd 0 Newbie Poster

ah cheers mate, much appreciated!!!
i need to think more logically - thanks again!

insamd 0 Newbie Poster

Hey everyone,
Im new here, i have been browsing round these forums for awhile and thought i would get involved. Anyway i have a slight problem... I have a little bit of code that changes a string (eg. john) into its binary representation.
What is going wrong, is that my code combines all the ASCII values for each letter instead of keeping them seperate and then converting each ASCII value to its binary equivalent.

Here is what i have got:

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void decToBin(int number, int base);
int main()
{
    int decimalNumber;
    int base;
    int answer=0;
    int digit;
    base = 2;
    cout<<"Enter a string: ";
    string name;
    getline(cin,name);
    for (int i=0; i<name.size(); i++)
    {
            answer = answer *10;
            digit = int(name[i])-int("0");
            answer = answer + digit;   
    }
 
    decimalNumber=answer;
    cout<<"Decimal "<<decimalNumber<<" = ";
    decToBin(decimalNumber, base);
    cout<<" Binary"<<endl;
    system("PAUSE");
    return 0;
}    
void decToBin (int number, int base)
{
     if (number>0)
     {
        decToBin(number/base, base);
        cout<<number%base;
     }
}

My output i am trying to get to is:
J binary (binary value here)
o binary (binary value here)
h binary (binary value here)
n binary (binary value here)

But as you can see i am struggling, any help would be appreciated thanks :)