Question

Write a MATLAB program to read in numbers from a file one at a time until...

Write a MATLAB program to read in numbers from a file one at a time until a negative even
number is read in or there are no numbers left to be read. Calculate the average of the numbers
that were read in, not including the negative even number. Remember to account for the case
where the first number read in is a negative even number.

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

%# open file for reading
fid1 = fopen('Input.txt','r');
numbersum = 0;
LineCount = 0;
while ~feof(fid1)
%# read line by line
line = fgets(fid1);
   LineCount = LineCount + 1;
  
   %# sscanf can read only numeric data
A = sscanf(line,'%d');
  
if ((A(1) < 0) && (mod(A(1),2) == 0))
   break;
end
   numbersum = numbersum + A(1);
end
fclose(fid1);

Average = numbersum / n;

disp('Average of the number is : ')
disp(Average)

Add a comment
Know the answer?
Add Answer to:
Write a MATLAB program to read in numbers from a file one at a time until...
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
  • Arrays and reading from a file USE C++ to write a program that will read a...

    Arrays and reading from a file USE C++ to write a program that will read a file containing floating point numbers, store the numbers into an array, count how many numbers were in the file, then output the numbers in reverse order. The program should only use one array and that array should not be changed after the numbers have been read. You should assume that the file does not contain more than 100 floating point numbers. The file name...

  • Write a program to read the contents of data.txt file, and determine the smallest value (min),...

    Write a program to read the contents of data.txt file, and determine the smallest value (min), largest value (max), and the average value (ave). The minimum, maximum, and average values must be calculated in the function calc(). Remember to declare the stdlib.h library, so that you can use the atof function, which will convert the number in the text file into float number. Also, remember to check if there is an error opening the file or not. If there is...

  • In C++ This program will read a group of positive numbers from three files ( not...

    In C++ This program will read a group of positive numbers from three files ( not all necessarily the same size), and then calculate the average and median values for each file. Each file should have at least 10 scores. The program will then print all the scores in the order that they were input, showing each number and what percentage it is above or below the average for each file. Note: Finding the average doesn’t require an array. Finding...

  • In this exercise, write a complete Java program that reads integer numbers from the user until...

    In this exercise, write a complete Java program that reads integer numbers from the user until a negative value is entered. It should then output the average of the numbers, not including the negative number. If no non-negative values are entered, the program should issue an error message. You are required to use a do-while loop to solve this problem. Solutions that do not use a do-while loop will be given a zero. Make sure to add appropriate comments to...

  • Write a C++ that read a list of numbers from a .txt file into array. The...

    Write a C++ that read a list of numbers from a .txt file into array. The first digit on the list will be array size, while the second number on the list will be our first number in our array list. Example: t1.txt file contain list off input number: 5, 3, 6, 10, 43, 23. notice the first number is 5 which will need to be use as an array size and the rest of the numbers are in the...

  • This program is in C++ Write a program that validates charge account numbers read in from...

    This program is in C++ Write a program that validates charge account numbers read in from the file Charges.txt following this assignment (A23 Data). Use a selection sort function on the array to sort the numbers then use a binary search to validate the account numbers. Print out the sorted list of account numbers so you can see the valid ones. Prompt the user to enter an account number and the validate that number. If the account number entered is...

  • 1- Write a program to read 10 numbers and find the average of these numbers. Use...

    1- Write a program to read 10 numbers and find the average of these numbers. Use a while loop to read these numbers and keep track of input numbers read. Terminate the while loop with a sentinel value. 2- Now modify the program to find the maximum of these numbers as well. The program should print the number of elements read as input and run until -1 is entered. (-1 is the sentinel that terminates the loop and the program)...

  • write a c++ code to read multiple integers from input.dat until the end of file. Edit...

    write a c++ code to read multiple integers from input.dat until the end of file. Edit input.dat and put several integers in it. Compile and execute the code then check output.dat to verify all the integers are in there. Update the code to check to see if input.dat exists before reading from it. If it doesn’t exist print an error message (and don’t read!). Compile and execute your code. Delete input.dat and output.dat and execute – did you see your...

  • using c++ write a program that reads numbers from the user until the user enters a...

    using c++ write a program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following: 1. Output the sum of all even numbers

  • write a program that will read a file containing floating point numbers, store the numbers into...

    write a program that will read a file containing floating point numbers, store the numbers into an array, count how many numbers were in the file, then output the numbers in reverse order. Your program should only use one array and that array should not be changed after the numbers have been read. You should assume that the file does not contain more than 100 floating point numbers. Numbers found in file 100.7362 105.2896 66.3493 105.6688 91.6180 64.8770 73.9249 87.3441...

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