[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 15 KB, 160x160, 1302116516012.jpg [View same] [iqdb] [saucenao] [google]
2954112 No.2954112 [Reply] [Original]

Hello /sci/,
please help me with this C++ program,
---------
Write a program that reads numbers from a file and find the maximum number

any hints solutions ?

>> No.2954120

I dont know, seems pretty tough, if not impossible

>> No.2954129

1. file type
2. >
3. profit

>> No.2954130

>>2954120
OP here,
i tried this :

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int max,x;
ifstream read;
read.open("example.txt")
if(!read.eof())
read>>max;

while(!read.eof())
{
read>>x;
if (x>=max)
max=x;
}
read.close();
return 0;
}
--------------------------------------------
Q3:
#include<iostream>
using namespace std;
int main()
{
int x,z;
cin>>x;
while (x>0)
{
z=x%10;
cout<<z;
x=x/10;
}
return 0;
}

--- but it didn't work, it just assumes the last number in the file as the max ..
idk whats wrong

>> No.2954131

cplusplus

input/output.

do some readong retard

>> No.2954136

>>2954130
sorry forget about Q3

>> No.2954143

>>2954131
Tell me whats wrong with my answer fagget.
Q2:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int max,x;
ifstream read;
read.open("example.txt")
if(!read.eof())
read>>max;

while(!read.eof())
{
read>>x;
if (x>=max)
max=x;
}
read.close();
return 0;
}
---
it didnt work, it just assumes the last value as max

>> No.2954150

OP is troll, the problem is undecidable

>> No.2954152

>>2954143
cout each x, you should be able to figure out easily what is happenning

>> No.2954155

>>2954143
cout each x, you should be able to figure it out easier

>> No.2954156

>>2954150
gtfo my /sci/ faggot, if you cant help with this then scramb

>> No.2954161

>>2954143
because you aren't comparing the numbers...

try making an array of the numbers so you can compare them easily.

>> No.2954162

Wow, that's just pathetic.
Seriously.
Why don't you ask help with calculating 1+1 while you're at it?

>> No.2954165

>>2954155
i tried, it prints all of them exept the 1st value.
man...

>> No.2954168

it's treating eof as a number

>> No.2954167 [DELETED] 

(apply max (call-with-input-file "C:\\Users\\ddohoda\\My Documents\\numbers.txt" read))

>> No.2954174

(apply max (call-with-input-file "C:\\Users\\david\\My Documents\\numbers.txt" read))

>> No.2954176

>>2954161
idk how to make one

>> No.2954178
File: 100 KB, 1000x1939, 1271207605648.png [View same] [iqdb] [saucenao] [google]
2954178

>>2954162

>> No.2954191

>>2954174
THIS THE PROGRAMING LANGUAGE MASTER RACE. C++ IS SHITSKINS

>> No.2954209

>>2954165
Try adding them together and print that out.

>> No.2954220

Ruby masterrace here:
print File.open("numdata.txt","r").read.scan(/\d+/).collect! {|n| n.to_i}.sort.last

>>2954191
What language would that be?

>> No.2954241

>>2954220

>Ruby programmer
>Doesn't know what LISP is

To be expected.

>> No.2954244

>Not using Python

Faggot

>> No.2954255

void main(void){
int input,max = -2147483648;

ifstream myReadFile;
myReadFile.open("text.txt");
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {


myReadFile >> input;
if(input > max)
max = input;


}
}
myReadFile.close();
cout << max;
}

>> No.2954261

int asdf(){

FILE *inp=fopen("whatever.txt");
int t,r;

fscanf(inp, "%d ", &r);
while(fscanf(inp, "%d ", &t) == 1) r = max(r,t);

fclose(inp);
return r;
}

>> No.2954271

>>2954220
> ruby
ruby and haskell are for hipster faggots

>> No.2954272

I got an idea, you can insert numbers into a std::set collection, it automatically sorts the numbers. First item is lowest, last highest. Just .insert(dem numbers) and relax

>> No.2954276

>using computers to solve problems
how quaint

>> No.2954289

>>2954244
>not using Java

double faggots

>> No.2954308

There are two correct ways to get input. One is to use C's "getchar" function. The other is to run a lisp and use "read." If C is too low-level, there is no reason to stop anywhere short of a lisp. Everything inbetween these two are just there because someone didn't read SICP.

>> No.2954346

>>2954308
<span class="math">\overline{\underline{{back~to~/prog/}}}[/spoiler]