Reading Data from a File into an Array Reading the contents of file into an arra
ID: 3816795 • Letter: R
Question
Reading Data from a File into an Array Reading the contents of file into an array is straightforward: the file and us to read each item from the file, storing each item in an array element. The a loo ate until either the array is filled or the end of the file is reached. Program loop should by opening a file that has 10 numbers stored in it and then reading the file's demonstrates into an array. Program 7-3 1 This program reads data from a file into an array. 2 include Kiostream> 3 finclude 4 using namespace std 6 int main 8 const int ARRAY SIZE 10; Array size 9 int numbers (ARRAY SIZE]: Array with 10 elements 11 int count 0; ifstream inputFile; Loop counter variable Input file stream object 13 ll open the file. 14 input File open("Ten Numbers .txt")Explanation / Answer
/*Assumptions: 1.Marks range for calculating grades is assumed.
2. filename 'example.txt' is to be replaced by your own filename*/
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
const int SIZE = 20;
int count = 0; //to get the count of marks in the file
int arr[SIZE];
char grade[SIZE];
ifstream File;
File.open("example.txt");
while(!File.eof())
{
File >> arr[count];
count++;
}
File.close();
for(int i=0;i<count;i++) //assigning grades to the students
{
if(arr[i]>=90)
grade[i]='A';
else if(arr[i]>=80 && arr[i]<90)
grade[i]='B';
else if(arr[i]>=70 && arr[i]<80)
grade[i]='C';
else
grade[i]='D';
}
cout<<"grades of students are: ";
for(i=0;i<count;i++)
{
cout<<"student "<<i+1<<" "<<grade[i]<<endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.