GIDForums  

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

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Nov-2004, 09:26
agentxx04 agentxx04 is offline
New Member
 
Join Date: Sep 2004
Posts: 12
agentxx04 is on a distinguished road
Unhappy

Something's wrong w/my program.


Hi. My program is supposed to find the median. Originally, I arranged the variable median to be int, but I realized that for medians of even sets of numbers, it would make more sense to set median to float. So here's what I got:

CPP / C++ / C Code:
 
#include<stdio.h>

int main()
{
	int item[100];
	int a, b, t;
	float median;
	int count;


	printf("How Many numbers do you want to enter? ");
	scanf("%d", &count);
	for(a=0; a<count; a++){
		scanf("%d", &item[a]);
		}
	for(a=1; a<count; a++)
		for(b=count-1; b>=a; --b){
			if(item[b-1] > item[b]){
				t=item[b-1];
				item[b-1] = item[b];
				item[b]=t;
			}
		}
	median = count/2;
	if(count%2 ==1){
		printf("There are odd sets of numbers.\n");
		median = item[median];

		}
	else{
	printf("There are even sets of numbers.\n");
	median = (item[median]+item[median-1])/2;
	}

	for(t=0; t<count; t++){
		printf("%d\n", item[t]);

	}
	printf("The Median is %d", median);

	return 0;
	}
 

But now, I can't run the program because there are 3 errors, all saying:

Compiling NONAME08.CPP:
Error NONAME08.CPP 27: Illegal use of floating point in function main()
Error NONAME08.CPP 32: Illegal use of floating point in function main()
Error NONAME08.CPP 32: Illegal use of floating point in function main()

What did I do wrong? Could anybody help me please?

Thank You.
  #2  
Old 07-Nov-2004, 10:03
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,709
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by agentxx04
Hi. My program is supposed to find the median. Originally, I arranged the variable median to be int, but I realized that for medians of even sets of numbers, it would make more sense to set median to float. So here's what I got:

CPP / C++ / C Code:
 
       /* stuff here */

	float median;
      /* more stuff here */

       median = item[median];
      
      /* more stuff here */

	median = (item[median]+item[median-1])/2;

      /* final stuff */
	
 

But now, I can't run the program because there are 3 errors, all saying:

Compiling NONAME08.CPP:
Error NONAME08.CPP 27: Illegal use of floating point in function main()
Error NONAME08.CPP 32: Illegal use of floating point in function main()
Error NONAME08.CPP 32: Illegal use of floating point in function main()

What did I do wrong? Could anybody help me please?

Thank You.


It's telling you that an array index must be an int. Now, for even sets of numbers the median value may very be a non-integer, as your program tries to compute. One way to handle it is to have an int variable for the array index, and a float variable for the median itself.

I think the logic in your program is OK; here's a suggestion for getting it to work:

CPP / C++ / C Code:
{
  int median_index;
  float median;

  /* all of your stuff OK up until: */


 
  if(count%2 ==1){
    printf("There are odd sets of numbers.\n");
    median = item[count/2]; /*int division gives middle point of the sequence */
  }
  else  {
    median_index = count/2;
    printf("There are even sets of numbers.\n");
    median = (item[median_index]+item[median_index-1])/2.0;/* Average of two middle points */
  }

   /* your stuff OK here */


    printf("The Median is %.1f", fmedian);

    return 0;
}

Regards,

Dave
 
 

Recent GIDBlogMeeting the populace 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
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 13:30
RSA program fwongmc C Programming Language 11 08-Nov-2004 22:15
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 20:25
Need help with a C program (Long) McFury C Programming Language 3 29-Apr-2004 20:06
one program access another? dgoulston C++ Forum 1 07-Oct-2003 11:26

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

All times are GMT -6. The time now is 16:15.


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