C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-25-2009, 01:37 PM   #16
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
Quote:
Originally Posted by ali_1234 View Post
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
what are you talking about?

fscanf reads from the "indata.txt"

what it has to do with your pressing enter?
__________________
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:46 PM   #17
Registered User
 
Join Date: Mar 2009
Posts: 11
i am getting an output file now but the problem is min value is always zero and max mum value is some number...0814...
now again im stuck ...
ali_1234 is offline   Reply With Quote
Old 03-25-2009, 01:47 PM   #18
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
Quote:
Originally Posted by ali_1234 View Post
i am getting an output file now but the problem is min value is always zero and max mum value is some number...0814...
now again im stuck ...
post your last code
__________________
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:51 PM   #19
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,"%d",&val))
		{
			a[n]=val;
			n=(n+1);
         printf("%d %d\n", val, n);
         getch ();


     
		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);
		getchar ();
		return 0;
	   }
         }
}
ali_1234 is offline   Reply With Quote
Old 03-25-2009, 01:57 PM   #20
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
your indentetion suxx big deal - you do not even see simplest bugs

also - when you have return inside if - you do not need else - it just adds unneded indentation level

Code:
#include <stdio.h>

int main (void)
{
	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;
	}

	while(fscanf(fi,"%d",&val) == 1)
	{
		a[n]=val;
		n=(n+1);
		printf("%d %d\n", val, n);
		if(n == 10)
			break;
	}


	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;
	}
	fprintf (fo,"Maximum element in an array : %d\n", max);
	fprintf (fo,"Minimum element in an array : %d\n", min);

	fclose(fo);
	fclose(fi);
	getchar ();
	return 0;
}
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-25-2009, 02:11 PM   #21
Registered User
 
Join Date: Mar 2009
Posts: 11
thanks people for ur kind support ...
and vart i am really sorry i am jus a beginner and my programming teacher sux .. so i dont know a damn about it .. wht evr i have studied is by my self ... i m really thank full for ur support cudn't do this without u ppl !!!..

THANK YOU PPL !!!!........... spl VART !!
ali_1234 is offline   Reply With Quote
Old 03-25-2009, 02:19 PM   #22
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
Quote:
Originally Posted by ali_1234 View Post
thanks people for ur kind support ...
and vart i am really sorry i am jus a beginner and my programming teacher sux .. so i dont know a damn about it .. wht evr i have studied is by my self ... i m really thank full for ur support cudn't do this without u ppl !!!..

THANK YOU PPL !!!!........... spl VART !!
BTW - you still have a bug in your for loop - you access 11 elements whule have only 10 in the array
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart 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 07:52 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