9 Replies - 6550 Views - Last Post: 12 May 2009 - 01:06 AM Rate Topic: -----

#1 iCode   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 15-January 09

User-Defined Function

Posted 09 May 2009 - 06:03 AM

I am creating a program consisting of four user-defined function each doing different task but showing results in the end. However, my code says that elements are undeclared. How can I link the functions?

#include<stdio.h>



void ReadMarks(float marks[]){
	 float i=0;
	 
	 printf("Please enter students marks (0000 to end): ");
	 while(1){
			  if (i==0000)
						   break;
			  scanf("%f", &marks[i]);
			  i++;
			  }
}
			  

float Average(float marks[]){
	  int sum=0, total=0;

	  ReadMarks(marks);
	  for(i=0;i<'\0'; i++){
					  sum+=marks[i]
					  total++}
							
	  return sum/total;
	  }
	  
void Results(float marks[]){
	 int M=0, P=0, F=0;
	 ReadTemp(temp);
	 
	 for(i=0;i<'\0'; i++){
					 if (marks[i]<35){
									 F++;}
					 else if(35<=temp[i]<=60){
									 P++;}
					 else{
									 M++;}
					 }
					 
	 printf("Merit Students: %d\nPass Students: %d\nFailed Students: %d", M, P, F);
}
						
	  
	  

int main()
{
	float marks[100] = {0};
	ReadMarks(marks);
	printf("Average = %f C\n", Average(marks));
	Results(marks);
	return 0;
}

This post has been edited by iCode: 09 May 2009 - 06:08 AM


Is This A Good Question/Topic? 0
  • +

#3 AndyH1963   User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 201
  • Joined: 22-June 08

Re: User-Defined Function

Posted 09 May 2009 - 06:33 AM

I would suggest that you tidy your code up and:

(1) Drop your K&R style, i.e. placing opening brace on same line as methiod name, condition etc, and adopt the ANSI C++ style where opening brace goes on a new line and braces are vertically aligned. You will then see that you are missing some semi-colons.

(2) Only use UPPER CASE variable names for constants.


You are using a method that you have not even declared i.e. ReadTemp

See what you end up with then.
Was This Post Helpful? 1
  • +
  • -

#5 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: User-Defined Function

Posted 09 May 2009 - 07:00 AM

I'm not sure how the location of your opening brace would help you here but you absolutely need to stop ending lines with closing braces.
The closing brace belongs directly under the opening character of the initiating statement.
Whether you place the opening brace in that same column (as AndyH1963 recommends) or on the same line as the initiating statement (as I prefer) does not change the fact of where the closing brace belongs.
(the "else if" is an exception which proves the rule).
You need to get that right to have code that is correctly formatted and therefore readable for the reasons AndyH1963 suggests.

Also use indentation consistently and correctly.
Your code was not correctly indented and that made it effectively unreadable for both you and us.
If you can't easily read your code you can't debug it.

So here is your code 'scrubbed' but uncorrected.
I have maintained the OTBS (One True Brace Style - or K&R style as AndyH1963 calls it).
Notice that I have used bracing even on your one line scope at "break;".
Do this. It's not required but it will pay you back many time over in increased readability and debugging ease.
I have left your errors in place.
I have added some line numbers for reasons that will become apparent.
/* 1*/  #include <stdio.h>
/* 2*/  
/* 3*/  void ReadMarks(float marks[]) {
/* 4*/  	float i=0;
/* 5*/  	printf("Please enter students marks (0000 to end): ");
/* 6*/  	while(1) {
/* 7*/  		if (i==0000) {
/* 8*/  			break;
/* 9*/			    }
/*10*/  		scanf("%f", &marks[i]);
/*11*/  		i++;
/*12*/  	}
/*13*/  }
/*14*/  	
/*15*/  float Average(float marks[]) {
/*16*/  	int sum=0, total=0;
/*17*/  	ReadMarks(marks);
/*18*/  	for(i=0;i<'\0'; i++) {
/*19*/  		sum+=marks[i]
/*20*/  		total++
/*21*/  	}
/*22*/  	return sum/total;
/*23*/  }
/*24*/  	  
/*25*/  void Results(float marks[]) {
/*26*/  	int M=0, P=0, F=0;
/*27*/  	ReadTemp(temp);
/*28*/  
/*29*/  	for(i=0;i<'\0'; i++) {
/*30*/  		if (marks[i]<35) {
/*31*/  			F++;
/*32*/  		} else if(35<=temp[i]<=60){
/*33*/  			P++;
/*34*/ 				} else {
/*35*/  				M++;
/*36*/  			}
/*37*/  		}
/*38*/  					 
/*39*/  		printf("Merit Students: %d\nPass Students: %d\nFailed Students: %d", M, P, F);
/*40*/  }
/*41*/  						
/*42*/  	  
/*43*/  	  
/*44*/  
/*45*/  int main() {
/*46*/  	float marks[100] = {0};
/*47*/  	ReadMarks(marks);
/*48*/  	printf("Average = %f C\n", Average(marks));
/*49*/  	Results(marks);
/*50*/  	return 0;
/*51*/  }



Here is what the compiler reports:
dic.c: In function ‘ReadMarks’:
dic.c:10: error: array subscript is not an integer
dic.c: In function ‘Average’:
dic.c:18: error: ‘i’ undeclared (first use in this function)
dic.c:18: error: (Each undeclared identifier is reported only once
dic.c:18: error: for each function it appears in.)
dic.c:20: error: syntax error before ‘total’
dic.c: In function ‘Results’:
dic.c:27: warning: implicit declaration of function ‘ReadTemp’
dic.c:27: error: ‘temp’ undeclared (first use in this function)
dic.c:29: error: ‘i’ undeclared (first use in this function)

What did your compiler report?
You did try to compile this before posting it didn't you?

Fix the compiler reported errors and warnings.
Scan your code for missing ";"
Write the function ReadTemp().

If you are still having trouble get back to us.

This post has been edited by janotte: 09 May 2009 - 02:31 PM

Was This Post Helpful? 1
  • +
  • -

#6 iCode   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 15-January 09

Re: User-Defined Function

Posted 11 May 2009 - 02:33 AM

/* 1*/  #include <stdio.h>
/* 2*/  float Average(float); void Results(float);
/* 3*/  void ReadMarks(float marks[]) {
/* 4*/		  float i=0;
/* 5*/		  printf("Please enter students marks (0000 to end): ");
/* 6*/		  while(1) {
/* 7*/				  if (i==0000) {
/* 8*/						  break;
/* 9*/					  }
/*10*/				  scanf("%f", &marks[i]);
/*11*/				  i++;
/*12*/		  }
/*13*/  }
/*14*/		  
/*15*/  float Average(float marks[]) {
/*16*/		  int sum=0, total=0;
/*17*/		  ReadMarks(marks);
/*18*/		  for(i=0;i<'\0'; i++) {
/*19*/				  sum+=marks[i];
/*20*/				  total++;
/*21*/		  }
/*22*/		  return sum/total;
/*23*/  }
/*24*/			
/*25*/  void Results(float marks[]) {
/*26*/		  int M=0, P=0, F=0;
/*27*/		  ReadMarks(marks);
/*28*/  
/*29*/		  for(i=0;i<'\0'; i++) {
/*30*/				  if (marks[i]<35) {
/*31*/						  F++;
/*32*/				  } else if(35<=marks[i]<=60){
/*33*/						  P++;
/*34*/						  } else {
/*35*/								  M++;
/*36*/						  }
/*37*/				  }
/*38*/										   
/*39*/				  printf("Merit Students: %d\nPass Students: %d\nFailed Students: %d", M, P, F);
/*40*/  }
/*41*/												  
/*42*/			
/*43*/			
/*44*/  
/*45*/  int main() {
/*46*/		  float marks[100] = {0};
/*47*/		  ReadMarks(marks);
/*48*/		  printf("Average = %f C\n", Average(marks));
/*49*/		  Results(marks);
/*50*/		  return 0;
/*51*/  }


10: array subscript is not an integer <--- I've defined 'i' already, what does it mean by this?
15: conflicting types for 'Average' <--- I only wrote the function prototype at the top so why does it intefere?
18: `i' undeclared (first use in this function) <--- I've cross referenced from the 'ReadMarks' so 'i' should be declared?
25: conflicting types for 'Results' <--- I don't understand what this mean.
29: `i' undeclared (first use in this function) <--- I've cross referenced. :blink:
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: User-Defined Function

Posted 11 May 2009 - 05:15 AM

Your reading skills need help, apparently, as well as your attention to detail.

View PostiCode, on 11 May, 2009 - 04:33 AM, said:

10: array subscript is not an integer <--- I've defined 'i' already, what does it mean by this?

Read the error...NOT AN INTEGER...is your declaration of i here
float i = 0;

an INTEGER???

---

View PostiCode, on 11 May, 2009 - 04:33 AM, said:

15: conflicting types for 'Average' <--- I only wrote the function prototype at the top so why does it intefere?

Does your prototype
float Average(float);

match your definition?
float Average(float marks[])


---

View PostiCode, on 11 May, 2009 - 04:33 AM, said:

18: `i' undeclared (first use in this function) <--- I've cross referenced from the 'ReadMarks' so 'i' should be declared?

Reread your book/notes on scope. A variable declared in one function is not visible inside another function.

---

View PostiCode, on 11 May, 2009 - 04:33 AM, said:

25: conflicting types for 'Results' <--- I don't understand what this mean.

Just like the other one.

---

View PostiCode, on 11 May, 2009 - 04:33 AM, said:

29: `i' undeclared (first use in this function) <--- I've cross referenced.

And again, just like the other.
Was This Post Helpful? 1
  • +
  • -

#8 iCode   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 15-January 09

Re: User-Defined Function

Posted 11 May 2009 - 08:39 AM

I've sorted the other problems but this one I don't understand.

View PostJackOfAllTrades, on 11 May, 2009 - 04:15 AM, said:

Does your prototype
float Average(float);

match your definition?
float Average(float marks[])


The prototype is for all values where as the definition is for the specific case. Within the {}, I have considered for the specific case of marks[]. What need to be altered? :blink:
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: User-Defined Function

Posted 11 May 2009 - 10:11 AM

With that prototype, you've told the compiler you're going to be defining a function called Average which takes a float as an argument and returns a float.

When you define the function, you've defined a function called Average which takes an array of floats as an argument and returns a float.

Can you see the difference now?
Was This Post Helpful? 1
  • +
  • -

#10 iCode   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 15-January 09

Re: User-Defined Function

Posted 11 May 2009 - 10:44 AM

View PostJackOfAllTrades, on 11 May, 2009 - 09:11 AM, said:

With that prototype, you've told the compiler you're going to be defining a function called Average which takes a float as an argument and returns a float.

When you define the function, you've defined a function called Average which takes an array of floats as an argument and returns a float.

Can you see the difference now?


I see. It differs for an array. I should thus change the prototype? Is the prototype supposed to be:
float Average(float [])


Or do I need to change the defined function?
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: User-Defined Function

Posted 11 May 2009 - 12:13 PM

Well, seeing as you're calling ReadMarks inside Average, and ReadMarks requires an array to be passed, what do you think needs to be changed?
Was This Post Helpful? 1
  • +
  • -

#12 iCode   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 15-January 09

Re: User-Defined Function

Posted 12 May 2009 - 01:06 AM

View PostJackOfAllTrades, on 11 May, 2009 - 11:13 AM, said:

Well, seeing as you're calling ReadMarks inside Average, and ReadMarks requires an array to be passed, what do you think needs to be changed?


The Average needs to be changed.

float Average(float [])


The code above says average containing a floating point array. I'm not seeing why that is wrong.

This post has been edited by iCode: 12 May 2009 - 01:07 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1