C++:The attached text file contains an unknown quantity of positive real numbers
ID: 3569690 • Letter: C
Question
C++:The attached text file contains an unknown quantity of positive real numbers. Write a program that reads the numbers into an array. Find the minimum and maximum value of the numbers. Write the following to an output file and/or the screen: a message displaying how many numbers there are (file and screen), a message displaying the range of the values (file and screen), and the values of the array in increasing order (file only).
This is the file that was given called databaseB.txt
32.37872383
55.25993075
4.09745916
82.52694961
34.03659796
2.874420333,
4.099897131
46.01270098
29.51059295
39.96759333
39.82418349
68.56256699
98.97597537
42.42826962
23.57118497
8.214222722
93.23331185
79.44575145
79.91684893
81.83414723
65.78785636
88.34757806
46.80301602
9.663987491
33.33368007
19.21088928
6.01864818
25.95996845
4.310511881
9.968206682
53.98061305
7.399636547
74.47071601
48.39221343
59.45808113
72.70774912
39.13876298
72.18999414
92.74114773
28.19676001
90.27424044
32.62020054
11.16095618
39.0384022
65.04695112
25.29882752
56.36174826
97.2308805
4.44463873
44.51597616
40.29463418
31.57376526
70.01088911
50.09356249
61.85194962
65.47593949
13.0449622
35.11024866
94.37949146
45.5875241
73.42157321
12.29204141
93.87462566
2.471584764
56.51629047
87.49404637
68.65085795
88.08731869
74.27352696
96.8385883
24.38164454
93.04625558
60.29811435
37.03921742
44.22685984
99.71693693
42.67432575
66.00891122
87.43629055
67.53511122
67.24668032
32.63479655
39.6941059
81.55353879
22.85756358
19.6044939
88.39875513
14.38416792
33.02936924
12.78157195
47.33652862
39.52237917
57.94718552
96.98539658
96.28976404
74.22149865
81.9649331
73.79772401
91.26195844
22.09611364
3.772373345
81.84781977
96.12144749
93.67315176
41.95068194
62.99042289
54.26671743
89.26425156
13.67210526
52.81768668
62.70836472
35.5307244
35.99864634
13.41818913
66.16585111
56.29435964
65.35241808
8.44559572
33.96832215
25.07739429
58.80813075
86.30020303
35.00943806
85.05709936
10.18491088
79.59888472
29.10599299
15.37875166
56.26696955
57.36329327
90.72500187
36.58292527
10.35198494
89.74599392
64.0329406
32.96123805
75.66934569
37.14208145
50.34306708
30.48375469
56.84907327
24.51457762
63.55585947
54.25094995
90.7793751
26.20542918
48.65519238
94.06867943
12.06865778
11.77333956
73.9672105
24.17827714
44.53680438
97.79158538
96.85437588
31.87517699
67.47252432
56.62015454
91.5296714
34.01965511
5.311935182
0.885248641
75.38468134
60.99353438
5.919951297
85.76937626
15.41651854
9.986244072
77.15245137
80.88501004
88.53392517
67.37492601
0.485181718
22.90293863
18.95237278
47.23070685
8.686175805
84.27665707
36.59448963
28.34240698
49.12677762
45.77073969
60.16594235
10.54460115
2.294286776
56.02622732
69.61634937
90.90312929
79.61521346
91.15004117
5.581503902
43.94527861
62.69886348
16.73080734
94.45532427
92.07670435
34.69205839
74.83646947
62.65533088
96.76331435
51.39254957
78.19130379
65.08863548
22.67846403
43.59525917
10.58540857
15.05966356
82.0661302
23.45880028
63.13565239
59.13304967
8.191254249
11.95414684
38.03957265
70.47144954
36.62668947
42.60696487
70.54387953
5.873662685
25.10951185
82.99662575
38.40073244
34.85910164
99.74851841
10.98769342
45.95815096
72.02957717
55.69365392
95.49635999
47.58143688
14.94579902
39.89319172
57.37643812
28.29186586
61.75610756
11.92528701
97.9302236
84.77438254
56.24562688
64.84364729
94.59151671
36.24974885
91.71259604
8.807448316
27.34018419
33.4920113
77.0311853
87.44335717
1.27923317
59.27420956
80.66984656
92.07347064
2.668095174
37.83966867
15.45360588
3.226921798
89.94110009
91.81760425
53.35831548
38.69937615
10.62633151
39.53909926
7.454273671
80.0014916
12.11146799
57.31281403
70.83423861
69.07654714
89.00223769
39.57086865
90.32267159
31.88239437
32.96503349
70.4689452
93.89785944
14.97551545
60.24154416
45.50947757
24.8399076
82.81063061
51.33379799
53.02337321
76.15436535
1.913723022
90.52309607
96.96365138
82.36238156
64.90260735
66.21510668
23.45959928
65.21245163
37.38514749
77.62146104
86.69008858
20.93289115
12.66456599
9.825525768
27.31951161
19.37012204
51.18064149
30.69653228
4.247972408
55.02339251
76.13093199
96.03696231
93.64256845
73.20449167
55.46638571
66.75941948
9.508948475
19.78886842
27.32640744
28.50003639
30.44718108
98.90913424
28.74603768
5.170295461
60.484993
36.82996325
23.90164602
91.51673646
87.84512998
53.22145135
68.89008279
60.95446526
72.81065909
19.50686335
11.68108437
42.35823055
92.78597742
83.55531987
85.41678227
76.02149311
34.988596
95.43161674
14.43877757
62.62440969
41.55817276
85.19309848
39.01321343
99.23929907
78.18619696
2.852125246
42.73011444
32.85403892
95.06575268
38.48608998
16.76444547
56.45034862
95.48504615
62.98611474
72.30244625
91.05229867
58.60624161
83.54071831
53.22542654
86.26186297
6.559129028
64.17608545
60.14530198
16.99539415
18.71036576
40.56593517
43.03413699
84.28952387
76.33115784
70.51579006
61.63752211
74.47853362
26.49475579
2.353659848
48.4554513
26.30409938
72.82630591
71.90794774
95.21128774
18.44759578
72.1862183
74.09014261
97.59273413
64.28885235
67.51699341
45.42674053
22.26438043
51.98219562
61.51504935
38.7984925
57.97780408
37.63239739
59.86116494
2.530753746
71.04671655
32.38992585
29.13091817
62.94494442
47.31630748
76.77401609
38.67471803
33.80527593
27.74072841
28.81519317
94.27368761
89.45500989
66.68816669
98.54823721
39.63818515
33.00550535
46.38414778
68.40602677
19.76492837
35.3427022
25.00158361
37.9711605
47.74530062
71.29680932
50.19629722
79.31486703
77.69728427
75.99794757
73.15620827
36.92197618
81.33447954
43.72240276
67.97740744
68.11252787
57.53763072
99.19424888
81.17958257
61.52934109
4.793528514
25.75523938
91.62309356
65.70470336
40.74627059
54.37679948
61.5292063
91.825932
29.79843302
57.92043331
85.97786238
43.05832968
1.560489688
95.22943204
81.28729884
96.32000603
78.45858995
65.93389836
88.93979648
89.94848938
33.12404207
62.19275117
54.78806754
20.20986067
21.56975338
12.86655326
88.79649315
66.92687275
2.525715044
87.25151438
82.70232629
44.56570109
5.113556732
56.3811139
25.23387485
18.44389577
17.61053892
4.675480042
3.478794629
23.35937989
40.9279776
28.56312887
63.73506344
87.82095072
77.25173982
88.67076513
87.5859274
17.71103182
77.43180612
24.68478168
42.02910454
87.16397296
72.52588585
83.64431065
47.16588716
52.03458049
75.1895427
64.45358925
31.15952112
99.12328362
79.38558835
15.51862166
83.68478843
80.39946286
56.27505615
41.79486394
46.20213088
72.4877533
0.615147791
58.41514865
41.21383757
97.74003011
35.73226376
47.32832494
0.603315742
95.11865624
63.09095907
8.723352148
56.83870215
90.11410582
45.6816512
20.81091475
26.09953598
63.22755454
75.714301
4.623736671
28.63967814
19.34523734
81.8398373
69.7828349
61.39109029
84.10715835
89.71143104
1.698874445
17.11912803
86.27592357
10.32011464
17.78829017
3.844700561
51.02273528
93.57604369
43.8488594
15.47285516
20.84709274
51.54607411
57.94316701
80.0124812
73.4729638
89.53919879
32.48382248
95.92631322
37.21415888
2.748456902
79.69318567
76.51995265
45.22304056
73.33512164
84.40632605
46.43048837
53.33025421
68.62056435
62.49019315
91.64693977
31.41980268
18.51909506
86.44129411
79.77816397
76.23886816
11.85577614
41.29796005
3.175223057
48.82968899
42.49483375
98.15701837
16.00881231
45.01917805
4.516579482
96.48102443
62.74711938
35.90353167
68.92267653
1.801674261
34.60707764
53.39272658
70.99800372
59.52987753
27.24844531
49.94329286
19.31445277
7.955144241
88.90750308
90.39961584
46.13378298
0.940757968
57.6806131
9.162302254
89.28439943
40.1506681
79.4780518
10.22698585
7.216502977
7.847245779
7.270533435
50.27815566
30.24422386
37.75626223
5.234467264
66.38136654
41.27423773
1.431817711
30.58671965
83.59853351
30.12569031
88.4362862
68.28407181
22.70430113
85.34318323
31.86234955
85.32199654
48.36203474
10.97108659
58.76974447
56.43973696
67.82679193
64.66974449
28.80979364
59.67177934
46.76184016
84.47889444
75.77071806
19.7220352
97.35238628
45.64323215
62.83137492
67.0067963
8.983170474
59.6358027
17.11971603
12.66559115
9.868987226
67.17294724
12.57440893
3.482545148
8.154818369
88.71010835
65.05530744
68.22621907
97.64187568
25.77657751
31.9094321
8.608247834
42.75396941
16.41957353
14.28148936
48.02212584
67.77919452
11.12000246
2.605723022
93.61380259
99.75078176
61.33281118
17.97296714
58.0590545
54.70995945
82.42970396
10.15248537
49.65504523
5.896160805
46.39068242
64.39941489
96.88545121
30.69895383
94.68540239
54.42435062
76.50513494
88.39861845
52.83148572
92.87776201
47.30914431
42.83565173
52.14010385
67.73727615
37.96670944
4.15600707
95.29784167
32.3452481
27.20869332
59.69739794
45.23735083
10.49566193
69.57628284
27.76449303
15.5473195
20.08263068
57.42503295
38.1185045
45.55115545
9.915159758
91.227577
55.12053415
60.1420445
36.79887945
59.20867488
44.84791582
7.203563132
92.76072218
86.22289409
54.75132962
6.593709925
69.03280004
43.89375264
30.24692725
68.77842017
71.40247528
40.01050979
82.77439246
81.30036305
61.45448863
8.84617006
84.28718608
6.517163042
73.03693046
11.89524645
6.493116269
47.72732065
66.37996361
49.48964245
26.09333864
13.13257501
51.63887022
63.4807578
61.80007785
58.65524075
47.5273252
94.6995545
28.48573356
77.54808798
93.45766022
59.02180547
64.53623364
40.5212228
86.10947448
51.02042676
82.1716842
78.70525102
43.23193143
39.97112369
5.47307387
79.30749367
43.76949918
98.64564619
78.14733039
76.47084686
62.57010559
77.51025462
16.14647071
95.10721567
1.414990972
60.5481987
48.66418923
9.937839397
18.84281198
12.48274502
81.11508224
7.69750481
41.80183541
24.2641569
47.69047464
37.85768785
66.77179684
25.1481414
85.436026
20.74648888
84.3385509
43.43524104
83.57122603
50.34942207
13.53578969
22.55318478
71.49365453
85.91921084
27.19554723
13.91077544
30.80501738
32.68032187
31.85036606
36.35059981
98.35131277
8.67999922
10.22469307
This is what I have so far, I only know the basics. I saw the vector in another example and in a book, but I do not think we have learned that though. Is there another way or a way to modify because this program does not work. Also. if you can explain why certain methods were used such as the vector please. Thank you! Please use array instead of vector.
/*The attached text file contains an unknown quantity of positive real numbers.
Write a program that reads the numbers into an array. Find the minimum and maximum value of the numbers.
Write the following to an output file and/or the screen: a message displaying how many numbers there are (file and screen),
a message displaying the range of the values (file and screen), and the values of the array in increasing order (file only).*/
#include <iostream>
#include <fstream>
using namespace std;
double range (double x[],int n);
double maxVal(double x[],int n);
double minVal(double x[],int n);
int main()
{
ifstream fin ("dataset.txt");
ofstream fout ("result.txt");
int num[0];
double maxVal(double x[],int n)
{
/* Declare variables. */
int k;
double max_x;
/* Determine maximum value in the array. */
max_x = x[0];
for (k=1; k<=n-1; k++)
if (x[k] > max_x)
max_x = x[k];
/* Return maximum value. */
return max_x;
}
/*-------------------------------------------------------------*/
/* This function returns the minimum value in an array x */
/* with n elements. */
double minVal(double x[],int n)
{
/* Declare variables. */
int k;
double min_x;
/* Determine minimum value in the array. */
min_x = x[0];
for (k=1; k<=n-1; k++)
if (x[k] < min_x)
min_x = x[k];
/* Return minimum value. */
return min_x;
}
double range (double x[],int n)
{
maxVal-minVal=range;
return range;
}
void sort(double x[], int n)
{
// Declare local objects.
int m;
double hold;
// Implement selection sort algorithm
for (int k=0; k<=n-2; ++k)
{
// Find position of smallest value in array beginning at k
m = k;
for (int j=k+1; j<=n-1; ++j)
{
if (x[j] < x]m])
m = j;
}
// Exchange smallest value with value at k
hold = x[m];
x[m] = x[k];
x[k] = hold;
}
// Void return.
return;
}
cout << "Smallest is " << min << endl;
cout << "Largest is " << max << endl;
fout << "Smallest is " << min << endl;
fout << "Largest is " << max << endl;
fout << "Range is " << range << endl;
fout.close();
system("pause");
}
Explanation / Answer
A=0;
x=0;
n=input('How many variables=');
disp('Enter the coefficients along with constants For instance if x+y+3z=-5 then enter 1 1 3 -5 each number followed by an enter not space');
for i=1:n
for j=1:n+1
A(i,j)=input('');
end
end
%pivoting
for i=1:n-1
for j=i+1:n
if abs(A(j,i))>abs(A(i,i))
T=A(j,:);
A(j,:)=A(i,:);
A(i,:)=T;
end
end
end
disp('After pivoting');
disp(A);
for k=1:n-1
for i=k+1:n
m=A(i,k)/A(k,k);
for j=k:n+1
A(i,j)=A(i,j)-m*A(k,j);
end
end
end
disp('Triangularize Form ');
disp(A);
if A(n,n)==0
disp('No unique solution');
end
x(n)=A(n,n+1)/A(n,n);
for j=n-1:-1:1
sum=0;
for i=1:n
sprintf('x%.0f=%.10f',i,x(i))
end
for i=1:n-j
sum=sum+A(j,n+1-i)*x(n+1-i);
end
x(j)=(A(j,n+1)-sum)/A(j,j);
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.