I already got part1. You are given a text file called \"Random_Data.txt\". The v
ID: 3606190 • Letter: I
Question
I already got part1.
You are given a text file called "Random_Data.txt". The very first line of the file contains only 1 number. This number indicates how many vectors are stored in this file. Every vector in this file is 1 x 5. Write a script that will add up all of the vectors in this file and save your answer in a variable called "Total". My Solutions Improve Your Solution Activity Summary You have submitted: 1 solution Maximum Submissions: Unlimited solutions Most recent best submission: 1st Submission: All tests passed Test Results Submitted 29 minutes ago Solution ID: 5562703 %write the code to complete this solution here. This should be a script and not a function. field = fopen ("Random-Data. txt','r'); count = fscanf ( field, '%d', [1,1]); Total = zeros ( 1 , 5 ) ; for i = 1 : count temp = fscanf ( field, '%d', TotalTotal+ temp; [1,5]); end fclose(field);Explanation / Answer
//This program is written in node js ,fs is a file system module which provides file manipulation functionality
var fs = require('fs');
//This line reads the contents of Input file syncronously and saves it in a variable called fileContents
var fileContents = fs.readFileSync('Random_Data.txt','utf8');
//Here I make fileContents an array by taking each entry or a line as a single entry in array
fileContents = fileContents.split(' ');
//variable total will store the final output
var total = 0;
for(var i=1;i<=fileContents[0];i++) {
total = parseInt(total) + parseInt(fileContents[i]);
if(i!=1) {
//This will apppend to the end of file - intermediate results
fs.appendFileSync('Results.txt',total+' ');
}
}
//This line prints out the total
console.log("The total of all is " + total);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.