Question

write a program that reads all values from a text file data.txt and stores them in ID array valuesl I. The input process frc++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <fstream>

using namespace std;

int EvenSumDigits(int val){

int sum = 0;

while(val !=0){

sum = sum + val%10;

val = val/10;

}

return sum%2;

}

void getValues(int *Arr, int n){

int evenArr[10000], val = 0, id = 0, idxArr[10000];

for(int i=0 ; i<n ; ++i){

if(!EvenSumDigits(Arr[i])){

evenArr[val++] = Arr[i];

idxArr[id++] = i;

}

}

for(int i=0 ; i<val ; ++i){

cout<<evenArr[i] <<" "<<idxArr[i] <<endl;

}

cout<<"Number of elements: "<<val<<endl;


}

int main() {

int values[10000], val, n = 0, choice;

ifstream iFile("data.txt");

if(!iFile)

{

cout << "Error! the requested file data.txt'" << "' was not found" << endl;

cout << "Exiting ..." << endl;

exit(-1);

}

int sum = 0, min = 1000000, max = -1000000;

while (iFile >> val){

if(val < 0)

break;

values[n++] = val;

}

int evenArr[10000], idval = 0, id = 0, idxArr[10000];

for(int i=0 ; i<n ; ++i){

if(!EvenSumDigits(values[i])){

evenArr[idval++] = values[i];

idxArr[id++] = i;

}

}

for(int i=0 ; i<idval ; ++i){

cout<<"Value = "<<evenArr[i] <<" id = "<<idxArr[i] <<endl;

}

cout<<"Number of elements: "<<idval<<endl;


getValues(values,n);


}





=======================================================
SEE OUTPUT
saved clang version 7.0.0-3-ubuntu0.18.04.1 (t<c 0/final) clang++-7 -pthread -o main main.cpp ./main Value-51 id1 Value-33 idFilesdtsaved data.txt clang version 7.0.0-3-ubuntu0.18.04.1 (tags/RELEA 0/final) clang++-7-pthread -o main main.cpp /mairn Va

Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
c++ write a program that reads all values from a text file "data.txt" and stores them in ID array valuesl I. The input process from the file should be terminated when a negative value is detec...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT