C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-25-2009, 03:51 AM   #1
Registered User
 
Join Date: Mar 2009
Posts: 11
please help

Code:
this is my school project i think i have almost completed it .. i have to input data from external file and write it in other out put file... i have to fine the max and min of the given data in the input file... 
the program is getting crashed.. please help

#include <stdio.h>

int main ()
{
  int i,a[10],val,n=0,max,min;
  FILE *fi,*fo;
	if((fi=fopen("indata.txt","r"))==NULL)
      {
		printf("Error in opening the file!!!\n");
       return 1;
      }

	else
{
	while(fscanf(fi,"%f",&val))
	{
		a[n]=val;
		n=n+1;
	}

 max = a[0];
  min = a[0];

  for (i = 0; i < 10; i++)
    {
      if (a[i] > max)
        {
          max = a[i];
        }
      else if (a[i] < min)
        {
          min = a[i];
        }
    }
	if((fo=fopen("result.txt","w"))==NULL)
      {
		printf("Error in opening the file!!!\n");
       return 1;
      }
	else
{
  fprintf (fo,"Maximum element in an array : %d\n", max);
  fprintf (fo,"Minimum element in an array : %d\n", min);
}

fclose(fo);


fclose(fi);

return 0;
}
}
ali_1234 is offline   Reply With Quote
Old 03-25-2009, 04:18 AM   #2
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
1. do not put regular text inside the code tags
2. indent your code
Code:
#include <stdio.h>

int main ()
{
	int i,a[10],val,n=0,max,min;
	FILE *fi,*fo;
	if((fi=fopen("indata.txt","r"))==NULL)
	{
		printf("Error in opening the file!!!\n");
		return 1;
	}
	
	else
	{
		while(fscanf(fi,"%f",&val))
		{
			a[n]=val;
			n=n+1;
		}
		
		max = a[0];
		min = a[0];
		
		for (i = 0; i < 10; i++)
		{
			if (a[i] > max)
			{
				max = a[i];
			}
			else if (a[i] < min)
			{
				min = a[i];
			}
		}
		if((fo=fopen("result.txt","w"))==NULL)
		{
			printf("Error in opening the file!!!\n");
			return 1;
		}
		else
		{
			fprintf (fo,"Maximum element in an array : %d\n", max);
			fprintf (fo,"Minimum element in an array : %d\n", min);
		}
		
		fclose(fo);
		
		
		fclose(fi);
		
		return 0;
	}
}
3. "%f" for floats - "%d" is for integers
4. in your while loop - check that you do not enter more than 10 numbers
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-25-2009, 05:57 AM   #3
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,139
Plus it's obvious you copied large portions. Not sure any sane person uses spaces and tabs.
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline   Reply With Quote
Old 03-25-2009, 10:01 AM   #4
Registered User
 
Join Date: Mar 2009
Posts: 11
i copied it from borland directly over here thtz y it showed like this and its a project so i have to make it presentable and readable .. so thats why its like this
ali_1234 is offline   Reply With Quote
Old 03-25-2009, 10:11 AM   #5
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,946
Quote:
Originally Posted by vart
I see horizontal scroll bar - and it is better than having all the page extended to the right
You're killing me with this one
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 03-25-2009, 10:37 AM   #6
Registered User
 
Join Date: Mar 2009
Posts: 11
still the progrm isn't working while compiling it juz blinks and goes away... and i dont get any out put file ... i evn tried 'getch ();' ... still nothing happnd ...
ali_1234 is offline   Reply With Quote
Old 03-25-2009, 10:46 AM   #7
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,946
The easiest way to debug something like this is to scatter some printf statements in appropriate places, like this:
Code:
	while(fscanf(fi,"%d",&val))
	{
		a[n]=val;
		n=n+1;
printf("%d %d\n", val, n);
	}
This will allow you to see what is actually happening as opposed to what you hoped was happening, and then you can deal with more specific problems, such as "why didn't fscanf work here?"

Don't bother indenting the debug printfs, since that makes it easier to go back and erase them all later.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 03-25-2009, 11:08 AM   #8
Registered User
 
Join Date: Sep 2006
Posts: 2,512
You may want to hold the console window open with some line of code like this:

Code:
printf("\n\n\t\t\t      press enter when ready ");
i = getchar();  //hold the console window open
and put those two lines lines of code just before your return line.
Adak is offline   Reply With Quote
Old 03-25-2009, 11:35 AM   #9
Registered User
 
Join Date: Mar 2009
Posts: 11
i tried all the above solution u people gave.. still when i compile i dont get any output nor it shows any error ...
thank you people for your support .. but i am still not getting any output is there somthing wrong in my code ??..
ali_1234 is offline   Reply With Quote
Old 03-25-2009, 11:55 AM   #10
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,946
Quote:
Originally Posted by ali_1234 View Post
i tried all the above solution u people gave.. still when i compile i dont get any output nor it shows any error ...
thank you people for your support .. but i am still not getting any output is there somthing wrong in my code ??..
If you cannot create a program which produces any output, you need to go back a few steps:
Code:
#include <stdio.h>

int main() {
     printf("HELLO WORLD!\n");
     return 0;
}
Try that, if it doesn't work then your problem is your IDE or OS, not C.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 03-25-2009, 12:01 PM   #11
Registered User
 
Join Date: Mar 2009
Posts: 11
no all other programs are compiling ... and the example u have given even tried that its also working...
but only my project program isn't working...
please help ... my mind is just stuck cant figure out any thing
ali_1234 is offline   Reply With Quote
Old 03-25-2009, 12:21 PM   #12
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,946
Quote:
Originally Posted by ali_1234 View Post
no all other programs are compiling ... and the example u have given even tried that its also working...
but only my project program isn't working...
please help ... my mind is just stuck cant figure out any thing
Then combine the two. Make HELLO WORLD the first line of your program and see if that prints. You can also pause the execution until you hit return with this:
Code:
getchar();
But if you use more than one, don't hit anything but enter (otherwise there will be characters left in the stdin buffer that will trip the next getchar).

Keep adding printf/getchar combinations until you reach the point where something unpredictable happens (like the program suddenly ends). I can tell you for sure that your code as originally posted will not do what you want, but probably only you can change that.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 03-25-2009, 01:17 PM   #13
Registered User
 
Join Date: Mar 2009
Posts: 11
Code:
#include <stdio.h>

int main ()
{
	int i,a[10],val,n=0,max,min;
	FILE *fi,*fo;
	if((fi=fopen("indata.txt","r"))==NULL)
	{
		printf("Error in opening the file!!!\n");
		return 1;
	}
	
	else
	{
		while(fscanf(fi,"%f",&val))
		{
			a[n]=val;
			n=n+1;
		}
the program is working perfectly fine till here but then it does not calculate max n minimum instead it goes on reading data from input file and finally crashes ... now what to do ?
ali_1234 is offline   Reply With Quote
Old 03-25-2009, 01:25 PM   #14
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
your fscanf could return 1 if one float value is successfully parsed
0 if nothing is parsed (for example string that is not a number encountered)
or EOF (which is -1 on most compilers) if the end-of-file riched.

what will happen to your loop in this case?
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-25-2009, 01:32 PM   #15
Registered User
 
Join Date: Mar 2009
Posts: 11
if i use return one after fscanf then its reading only the fist number from input file and after i press enter the program terminates...
i m using borland as my compiler
ali_1234 is offline   Reply With Quote
Reply

Tags
c programming, home work, input output, min and max, minimum and maximum

Thread Tools
Display Modes

Forum Jump


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


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22