Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,499 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,371 people online right now. Registration is fast and FREE... Join Now!




calculating max and min values

 
Reply to this topicStart new topic

calculating max and min values, I have to now use data entered from another text document and also pri

jrt6
28 Feb, 2007 - 10:13 AM
Post #1

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 16


My Contributions
I have written a program that will take the max and min of 10 set values. I have to now change my program to allow for these inputs to be entered in by another document, and print the values into another document.

Code:
#include<stdio.h>
int
main ()
{
int i;
int a[10] = { 10, -99, 888, 4, 234, 1, 30, 40, 22, 34 };
int max = a[0];
int min = a[0];


for (i = 0; i < 10; i++)
{
if (a[i] > max)
{
max = a[i];
}
else if (a[i] < min)
{
min = a[i];
}
}
printf ("Maximum : %d\n", max);
printf ("Minimum : %d\n", min);

return 0;
}

/* This is the section I have been trying to add but I dont no where, can someone help me please. */

FILE* fp;
fp = fopen("mydata.txt", "r");

FILE* fp;
fp = fopen("output.dat", "w");

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Calculating Max And Min Values
28 Feb, 2007 - 10:21 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
There are good tutorials here:

http://www.cplusplus.com/doc/tutorial/files.html

http://www.cpp-home.com/FileIO_tutorial.php


User is offlineProfile CardPM
+Quote Post

jrt6
RE: Calculating Max And Min Values
2 Mar, 2007 - 04:19 AM
Post #3

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 16


My Contributions
I have made a few changes to this program but I still dont understand it. Can someone help me please. I am also having problems with the two files, they are saved as dat files and I cannot input numbers into the files.

Thanks for your time

wub.gif wink2.gif cool.gif
#include<stdio.h>
int main ()

{
FILE *fi;
FILE *fo;

fi = fopen("infile.dat","r");
fo = fopen("outfile.dat","w");



{

FILE *fi; /* input file pointer */
if ( (fi=fopen("infile.dat","r")) == NULL ) {
printf("ERROR opening file!\n");
return 1; /* terminates program in error */
}

{
int i;
int a[10] = { fi };
int max = a[0];
int min = a[0];
for (i = 0; i < 10; i++)
}
{
if (a[i] > max)
{
max = a[i];
}
else if (a[i] < min)
{
min = a[i];
}
}
fprintf (fo,"Maximum : %d\n", max);
fprintf (fo,"Minimum : %d\n", min);

fclose(fo);
fclose(fi);

return 0;
}
}
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 06:18PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month