GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Mar-2009, 23:15
tpetsovi tpetsovi is offline
New Member
 
Join Date: Feb 2009
Posts: 7
tpetsovi has a little shameless behaviour in the past

Last function called IsPrimeNumber will not work correctly


The last function called IsPrimeNumber will not work correctly...any help will be appretiated...Thanks...

CPP / C++ / C Code:
/******************************************************************
Name :                       
Date :
Homework # :  Homework #6 Problem #1
Source  :    
Action  :  Menu is displayed allows user to calculate
		   Miles per Gallon, Total minutes and seconds
		   and determine if number is a prime number or not

Notes  :
********************************************************************/
#include <iostream>
#include <stdlib.h>      // this header file requires the .h
using namespace std;

void DisplayMenu(void);
void FindMilesPerGallon(void);
void ConvertTotalSeconds(void);
void IsPrimeNumber(void);


void main()
{
  int Choice;

  system("cls");	// this clears the output screen

  DisplayMenu();
  cin >> Choice;
  while (Choice != 4)
    {
      switch (Choice)
        {
	      case 1: FindMilesPerGallon();
			      break;
		  case 2: ConvertTotalSeconds();
				  break;
		  case 3: IsPrimeNumber();
				  break;
		  default : cout << "Sorry illegal choice, Please try again\n\n";
        }
      DisplayMenu();
      cin >> Choice;
	 
    }
}

/*********************  DisplayMenu  ************************************
Action  :  This just displays the menu to the screen
Parameters  : none
returns     : nothing
***********************************************************************/
void DisplayMenu()
{
  cout << "\n\nDo you want to:\n";
  cout << "   1) Find miles per gallon\n";
  cout << "   2) Convert total seconds to minutes and seconds\n";
  cout << "   3) Determine if number is a prime number or not\n";
  cout << "   4) QUIT\n\n";
  cout << "   CHOICE --> ";
  
}

/********************  FindMilesPerGallon  ******************************
Action  : Ask user to input number of miles traveled and number of
          gallons used then display the miles per gallon obtained.
Parameters : none
returns    : nothing
------------------------------------------------------------------------*/
void FindMilesPerGallon(void)
{
  float Miles = 0, Gallons = 0, MPG;

  cout << "\n\nPlease enter the number of gallons used--> ";
  cin >> Gallons;
  cout << endl << endl;
  cout << "Please enter miles traveled--> ";
  cin >> Miles;
  cout << endl << endl;

  MPG = Miles / Gallons;

  cout << "Your MPG is--> " << MPG << endl << endl;

}

/******************** ConvertTotalSeconds *********************
Action  : Ask user to input a given total number of seconds and then
          displays the corresponding number of minutes and seconds
Parameters : none
returns    : nothing
-------------------------------------------------------------------------*/
void ConvertTotalSeconds (void)
{
	int Seconds, Minutes, TotalSeconds = 0;
	
	cout << "\n\nPlease enter total seconds to convert--> ";
	cin >> TotalSeconds;
	cout << endl << endl;

	Minutes = TotalSeconds / 60;
	Seconds = TotalSeconds % 60;

	cout << "There are " << Minutes << " minutes and " << Seconds << " seconds.";
	cout << endl << endl;
}

/*********************** IsPrimeNumber()****************************
Action  : Ask user to input positive integer and will determine if number
          is a prime number or not.
Parameters  : none
Returns     : nothing
-------------------------------------------------------------------*/

void IsPrimeNumber (void)
{
	int Num, i;
	
	cout << "\n\nPlease enter number to check if it's prime--> ";
	cin >> Num;
	cout << endl << endl;
	
	for (i = 2; i <= Num; ++i)
	{
		;
	}
		if (Num % i == 0)
		{
			cout << Num << " is not a prime number.\n\n";
		}
		else if (Num % i != 0)
		{
			cout << Num << " is a prime number.\n\n";
		}
		else
		{
			cout << "Error with input.\n\n";
		}
	
}
Last edited by admin : 04-Mar-2009 at 01:55. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 04-Mar-2009, 07:34
dlp dlp is offline
Member
 
Join Date: May 2006
Posts: 157
dlp has a spectacular aura about

Re: Last function called IsPrimeNumber will not work correctly


CPP / C++ / C Code:
for (i = 2; i <= Num; ++i)
	{
		;
	}
		if (Num % i == 0)
		{
			cout << Num << " is not a prime number.\n\n";
		}
		else if (Num % i != 0)
		{
			cout << Num << " is a prime number.\n\n";
		}
		else
		{
			cout << "Error with input.\n\n";
		}

Well, you're not actually figuring out if the number is a prime or not. Currently, you loop from i = 2 to Num, and after the loop you test Num against the current value of i, which should be Num+1. How are you trying to figure out if a number is a prime or not?
  #3  
Old 04-Mar-2009, 13:02
zalezog zalezog is offline
Junior Member
 
Join Date: Oct 2007
Posts: 33
zalezog will become famous soon enough

Re: Last function called IsPrimeNumber will not work correctly


Quote:
CPP / C++ / C Code:
for (i = 2; i <= Num; ++i)
	{
		;
	}
//Why are you running the 'for' loop,if you aren't checking if the number   
 //    is divisible by some number or something of that sort?

If you find a number which is divisible by 'i' while iterating from 'i' to num-1, then the number is surely NOT prime and you immediately cout the statement "No: not prime" and return
else you go on ,if you come out of the loop successfully the number is surely prime.
CPP / C++ / C Code:
if (Num > 1)
{
for ( i=2 ;i < Num ;i++ )
  {
   if( Num % i ==0 ) //its not prime
    {
     cout <<"Not prime";
      return; //return to calling function
    } 
  }
//If you happen to reach here,then the number is surely prime.
 cout << "No:prime";
}

This is certainly not the fastest and there are better ways.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
run script command on ns2.26 newbie06 Computer Software Forum - Linux 65 19-Aug-2009 08:50
Problem executing nam-1.13 RodolfoAlvizu Computer Software Forum - Linux 20 28-Feb-2009 16:23
Flex and bison coding lucky88star C++ Forum 5 24-Dec-2007 12:57
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 17:19
Random access files Krazy_yA C Programming Language 6 07-May-2006 02:44

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:43.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.