Jump to content


Check out our Community Blogs

Willum

Member Since 30 Jan 2012
Offline Last Active Mar 15 2012 10:36 AM
-----

Topics I've Started

Finding the mode of a set of data

15 March 2012 - 10:31 AM

Hi everyone; I am currently working on a project that uses functions to find the mean, median, and mode of a set of data stored in a text file that is put into an array. I wrote everything but the mode, as I have no idea how to find the mode, though! Any ideas? I'm sure its really simple and I'm just over thinking it.

Thanks in advance,
Willum

Reading information from a .txt file

04 February 2012 - 02:51 PM

So I'm trying to recreate a problem for a little bit of a challenge, which is basically a DNA codon to amino acid converter. I thought it would be relatively simple, it pulls the codons from a .txt file (dna.txt) and then going three characters at a time (which is a codon) it uses if statements to match the codon up to the codon relating to the amino acid and writes it to another text file. I know that's not a very 'effecient' route to take, but its all I know how to do at the moment (if anyone has an easier method for beginners and cares to explain it in layman's terms that'd be great too!). But the main part of the problem I'm having right now is the fact that my while loop to pull the text from doesn't work. Individually it works, but together it doesn't! Would anyone care to help out a failure in distress?

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){

char codon[4];
ifstream a_file("dna.txt");
/*
//This works individually
a_file.get(codon, 4);
cout<<codon;                    
*/ 

//But with just the loop, it doesn't!!
a_file.get(codon, 4);
while(!a_file.eof()){
    cout<<codon;
    a_file.get(codon, 4);
    }
}

Thank you so much in advance!

Prime Factorization

01 February 2012 - 01:29 PM

So, I'm following a book tutorial (C++ Without Fear, Second Edition) and one of the examples, is to better understand recursive functions is prime factorization. I understand the gist of it, I think, but whenever I run the program, it doesn't seem to work! Can anyone tell me what's going on?

#include <iostream>
#include <cmath>

using namespace std;

void get_divisors(int n);

int main(){

int n;

cout<<"Enter a number: ";
cin>>n;
get_divisors(n);
}

void get_divisors(int n){

int i;
double j = sqrt(n);

for(i=2; i<=j; i++){
    if (n % i == 0){
        cout<<i<<", ";
        get_divisors(n / i);
        return;
        }
    }
}

My only question about the program pertains to recursive functions, in general. When does the function end? Is it right before the 'recursive' part of the function ends? What I'm basically asking is this all that's executed:

int i;
double j = sqrt(n);

for(i=2; i<=j; i++){
    if (n % i == 0){
        cout<<i<<", ";

Thanks in advance!

Stuck in Loop

30 January 2012 - 01:10 PM

Hey guys, I have a simple program that does one of two things. It either tells you if a number is prime or not or it tells you what the NEXT prime number is. However the while loop in the beginning, which checks to make sure you typed in the correct string or not won't exit out if you typed in the correct string. It compiles fine, but as soon as it hits the loop its screwy.

Here's the code:
#include <iostream>
#include <cmath>
#include <string>

using namespace std;
//function is being prototyped!
bool prime(int n);

int main(){

int i;
string selection;
cout<<"This program features two different programs, a prime number checker and a next prime number finder.\n";
cout<<"What program would you like to use?";
cout<<"\nType checker for the prime number checker or finder for the next prime number finder: ";
cin>>selection;

//makes sure you typed the right thing
while((selection != "finder") || (selection != "checker")){
    cout<<"Can you not read? Type checker or finder!";
    cin>>selection;
}

//Checker Program
if(selection == "checker"){
cout<<"Prime Number Checker: V1.0\nWritten by Willum.";
cout<<"\n\nEnter a number to see if its prime or composite: ";
cin>>i;
if(prime(i)){
    cout<<"\n"<< i <<" is prime!";
    }else{
        cout<<"\n"<<i<<" is composite!";
        }

    //Finder Program
    }else{
        cout<<"Next Prime Number Finder: V1.0\nWritten by Willum.";
        cout<<"\n\n Enter a number to find the next prime number: ";
        cin>>i;
        for(i; ; i++){
            if(prime(i)){
            cout<< i << " is the next prime number!";
            break;
            }
        }

    }
}
//n = i in the function. what goes in it, basically.
//the actual prime number function
bool prime(int n){
    int i;
    //prime number formula in a loop
    for (i=2; i<= sqrt(n); i++){
        //if there is no remainder
        if (n % i == 0){
            //its composite
            return false;
        }
        //can't find one, returns prime
    }
    return true;
}

Salutations!

30 January 2012 - 12:27 PM

Hello Everyone,

Just felt like making a short introduction, to introduce myself to the community! I'm a web developer turning programmer, who is currently trying to learn C++ reading books (oh joy...), and soon to be competing in programming for my school. I'll probably have many questions, as I tend to go astray from the excercises, trying to incorporate design elements into a terminal-based program (can you blame me, knowing my background)? I hope to help this thriving community as much as you'll surely help me. If you have any questions, feel free to ask me, as I'm a pretty easy-going guy.

In life, I love to collect Zippos, design things for friends, scuba dive, and be classy.

But enough of me,

-William

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download