This program I wrote has the following error:

(33,5):Declaration syntax error
(64,2):Declaration missing ;

#include <stdio.h>
#include <conio.h>

int range1(int a);
int range2(int b);
int range3(int c);
int range4(int d);

int main()

{
	int in, ctr, ctr1=0, ctr2=0, ctr3=0, ctr4=0, ctr5=30;
   for(ctr=1;ctr<=30;ctr++)
   {
      printf("Enter %d integer values within 1-200: ", ctr5--);
		scanf("%d", &in);

   }


   clrscr();
   ctr1=range1(in);
   ctr2=range2(in);
   ctr3=range3(in);
   ctr4=range4(in);
   printf("\nNumbers inputted within the range of 1-50 = %d", ctr1);
	printf("\nNumbers inputted within the range of 51-100 = %d", ctr2);
	printf("\nNumbers inputted within the range of 101-150 = %d", ctr3);
	printf("\nNumbers inputted within the range of 151-200 = %d", ctr4);
	getch();

	int range1(int a)
   {
      int a, ans1;
		if((a>=1)&&(a<=50))
			ans1=ans1+1;
         return ans1;
	}

	int range2(int b)
	{
   	 int b, ans2;
		 if((b>=51)&&(b<=100))
			ans2=ans2+1;
         return ans2;
	}

	int range3(int c)
	{
      int c, ans3;
		if((c>=101)&&(c<=150))
			ans3=ans3+1;
         return ans3;
	}

	int range4(int d)
	{
      int d, ans4;
		if((d>=151)&&(d<=200))
			ans4=ans4+1;
     		return ans4;
	}
 
}

Heres my another code. The problem is that its output is incorrect. It outputs wrong count of the numbers.

#include <stdio.h>
#include <conio.h>

int range1(int a);
int range2(int b);
int range3(int c);
int range4(int d);

int main()

{
	int in, ctr, ctr1, ctr2, ctr3, ctr4, ctr5=30;
   for(ctr=1;ctr<=30;ctr++)
   {
      printf("Enter %d integer values within 1-200: ", ctr5--);
		scanf("%d", &in);
      if((in>200)||(in<1))
      printf("Invalid Input\n");

   }


   clrscr();
   ctr1=range1(in);
   printf("\nNumbers inputted within the range of 1-50 = %d", ctr1);
   ctr2=range2(in);
   printf("\nNumbers inputted within the range of 51-100 = %d", ctr2);
   ctr3=range3(in);
   printf("\nNumbers inputted within the range of 101-150 = %d", ctr3);
   ctr4=range4(in);
   printf("\nNumbers inputted within the range of 151-200 = %d", ctr4);
   getch();
   return 0;
}

	int range1(int a)
   {
      int ans1=0;
		if((a>=1)&&(a<=50))
			ans1=ans1+1;
         return ans1;
	}

	int range2(int b)
	{
   	 int ans2=0;
		 if((b>=51)&&(b<=100))
			ans2=ans2+1;
         return ans2;
	}

	int range3(int c)
	{
      int ans3=0;
		if((c>=101)&&(c<=150))
      	ans3=ans3+1;
       	return ans3;
	}

	int range4(int d)
	{
      int ans4=0;
		if((d>=151)&&(d<=200))
			ans4=ans4+1;
     		return ans4;
	}

The problem is that its output is incorrect. It outputs wrong count of the numbers.

in only holds a single value at any time. When the loop ends, in contains the last value you typed and all of the others were thrown away. in should be an array if you want to work with a list of values, and each of the range() functions should loop through the array checking for values in the expected range.

But since you do the same thing in each range() function just with a different range, you can roll it all up into one function:

/* count values in the range of [lo..hi] */
int CountInRange(int a[], int sz, int lo, int hi)
{
    int x, cnt = 0;

    for (x = 0; x < sz; ++x)
    {
        if (a[x] >= lo && a[x] <= hi) ++cnt;
    }

    return cnt;
}

I dont get it. Can you modify the code sir? Or just tell me the instructions.

Can you modify the code sir? Or just tell me the instructions.

You have the range() function done since I wrote it for you, and I gave you an idea of how to change your main() function, but if I give you the code you will not learn as much as working your way through something you do not understand. You would have to learn that lesson at some point anyway, so it might as well be now. :)

We haven't discuss arrays in our class yet. Thats why I have few knowledge with C Programming. We are still stuck in functions. Maybe next week we will start Arrays.

If arrays has been not taught in the class why you want to use it? If you really want to learn something, then work on it. Guys here are ready to help you but first you must help yourself.

This article has been dead for over six months. Start a new discussion instead.