Problem With Terminal Window

Not sure if it's my program or computer

Page 1 of 1

7 Replies - 795 Views - Last Post: 28 October 2007 - 11:09 PM Rate Topic: -----

#1 garc2541  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-October 07

Problem With Terminal Window

Post icon  Posted 28 October 2007 - 06:44 PM

Everytime I run my program and the terminal window comes out, a window pops up saying pgm5.exe has encountered a problem and needs to close. Is this my program or my computer?
Is This A Good Question/Topic? 0
  • +

Replies To: Problem With Terminal Window

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2246
  • View blog
  • Posts: 9,236
  • Joined: 18-February 07

Re: Problem With Terminal Window

Posted 28 October 2007 - 06:58 PM

is pgm5 the name of your program?
Was This Post Helpful? 0
  • +
  • -

#3 garc2541  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-October 07

Re: Problem With Terminal Window

Posted 28 October 2007 - 07:09 PM

View PostNickDMax, on 28 Oct, 2007 - 06:58 PM, said:

is pgm5 the name of your program?


yes it is
Was This Post Helpful? 0
  • +
  • -

#4 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Problem With Terminal Window

Posted 28 October 2007 - 07:32 PM

Without further info, it's pretty tough to say anything about what's going on - can you post the code for your program?
Was This Post Helpful? 0
  • +
  • -

#5 garc2541  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-October 07

Re: Problem With Terminal Window

Posted 28 October 2007 - 07:35 PM

#include <iostream>
using namespace std;



int sumsq(int);
int sumcb(int);
int prime(int);


int main()
{
cout<<" Number	SumSq	  SumCb		PRIME "<<endl;
cout<<endl;

	
int numb;
int sum;
int sum1;
int count = 0;
int sum2 = 0;
int count1 = 0;







for(numb=5;numb<=49;numb+=2)
  
	
{	 
	  
	  if(prime(numb)==1)
	   cout<<"									 "<<"*"<<endl;
	  else 
	  cout<<endl; 
	 
   
cout<<"  "<<numb<<"		   "<<sumsq(numb)<<"		 "<<sumcb(numb)<<endl;


  sum2 = sum2 + numb;
  count = count ++;
  sum = sumsq(numb);
  sum1 = sumcb(numb);


}


cout<<endl;
cout<<"The Average of the Numbers is: "<<sum2/count<<endl;
cout<<"The Average of the sum of squares is: "<<endl;
cout<<"The Average of the sum of cubes is: "<<endl;
cout<<"The Number of the prime number is: "<<count1<<endl;



system ("pause");
return 0;
}

int sumsq(int numb){

int yoyo;
int sum = 0;


	  for(yoyo=1;yoyo<=numb;yoyo++)

	  sum+= yoyo*yoyo;
	  return sum;
}


int sumcb(int numb){

int val;
int sum1 = 0;


   for(val=1;val<=numb;val++)

	 sum1+= val*val*val;
	 return sum1;
}   



int prime(int numb) {

int p;



	for (p=2; p<=numb/2; p++) {
		if (numb%p==0)  {
			return 0;
		}
	}
		return 1;
	}


This post has been edited by garc2541: 28 October 2007 - 07:36 PM

Was This Post Helpful? 0
  • +
  • -

#6 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Problem With Terminal Window

Posted 28 October 2007 - 08:04 PM

count = count ++;

is not the proper form to increment a variable by 1 - you can use any of the following instead:
count++;  //postfix increment
++count;  //prefix increment
count+=1;  //assignment by addition
count=count+1;  //assignment of the result of addition

The reason that you are having the program crash is that your code is not incrementing the count variable properly, so its value remains at 0. When you try to output sum2/count, you are trying to output something divided by zero. The cout object doesn't handle infinities gracefully, and the program crashes.

If you change the increment step in your loop to any of the lines given above, it should work.

Incidentally, for calculating averages, the use of ints is generally not recommended - any values after the decimal place resulting from division will be truncated, giving you potentially unexpected answers (e.g. 1/3=0, 27/4=6, etc). You may want to change these to floats or doubles, or cast them as doubles when you're doing the output step.

Hope that helps,

-jjh

This post has been edited by jjhaag: 28 October 2007 - 08:05 PM

Was This Post Helpful? 0
  • +
  • -

#7 garc2541  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-October 07

Re: Problem With Terminal Window

Posted 28 October 2007 - 10:44 PM

View Postjjhaag, on 28 Oct, 2007 - 08:04 PM, said:

count = count ++;

is not the proper form to increment a variable by 1 - you can use any of the following instead:
count++;  //postfix increment
++count;  //prefix increment
count+=1;  //assignment by addition
count=count+1;  //assignment of the result of addition

The reason that you are having the program crash is that your code is not incrementing the count variable properly, so its value remains at 0. When you try to output sum2/count, you are trying to output something divided by zero. The cout object doesn't handle infinities gracefully, and the program crashes.

If you change the increment step in your loop to any of the lines given above, it should work.

Incidentally, for calculating averages, the use of ints is generally not recommended - any values after the decimal place resulting from division will be truncated, giving you potentially unexpected answers (e.g. 1/3=0, 27/4=6, etc). You may want to change these to floats or doubles, or cast them as doubles when you're doing the output step.

Hope that helps,

-jjh



thanks jjh what you said was the problem. Quick question if I want to find the avg of the sum of sq in my program do i call the count and do the count in the function or outside the function?
Was This Post Helpful? 0
  • +
  • -

#8 harshakirans  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 124
  • Joined: 26-April 06

Re: Problem With Terminal Window

Posted 28 October 2007 - 11:09 PM

Define the functions "sumsq..etc" which you want to call in the main before the defnition of main()

Or


delcare(write prototype of) those functions before the main()

and you can call those functions inside the main();
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1