Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I. Read input integers from a file into an array A; there will be up to 500 elem

ID: 3730370 • Letter: I

Question

I. Read input integers from a file into an array A; there will be up to 500 elements. Then fill two new arrays with elements selected from A as indicated below Array A13 will contain all the elements of A that are multiples of 13. Array Aeven will contain all the elements of A that are even integers. Output the contents of these arrays, with labels, to a file. Also output the number of elements in each array 2. Lab Assignment: Declare an array of C-strings to hold course names, and read in course names from an input file. Then do the output to show each course name that was read in. See Chapter 8 section on "C-Strings", and the section "Arrays of Strings and C-strings", which gives an example related to this lab. Declare the array for course names as a 2-D char array: char courseNames[ 10][50]; (allows for 10 courses and up to 49 characters in a course name) each row will be a C-string, and you can use it by writing courseNames[i] as if the array were one-dimensional! Use infile.get(courseNameslil], 50); for reading C-string data row-by-row; the get function will put in the null character after the input data for you. Note: The.get function reads characters into the array across an input file line, until it encounters a newline character. It does not remove the newline character from the input stream, so in order to read the next line, your program must do that. The textbook suggests declaring char discard; and using infile.get(discard); to read the newline character into the variable discard.

Explanation / Answer

Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.

//program1.cpp

#include<iostream>

#include<fstream>

using namespace std;

/*

* Program to read a file of integers, store them into an array,

* find the numbers divisible by 13, add them to an array,

* find the even numbers, add them to an array and output them all

* into a file

*/

int main(){

                //defining array to store numbers

                int numbers[500];

                //total numbers counter

                int count=0;

                //initializing input file

                ifstream inFile("integers.txt");

                if(!inFile){

                                cout<<"Cannot open input file";

                                return 1;

                }

                int i;

                //reading each and every integers, adding to the array

                while(inFile>>i){

                                numbers[count]=i;

                                count++;

                }

                //defining arrays for storing multiples of 13 and even numbers

                int A13[count];

                int Aeven[count];

                int a13Count=0;//multiples of 13 counter

                int aEvenCount=0;//even numbers counter

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

                                if(numbers[i]%13==0){

                                                //divisible by 13

                                                A13[a13Count]=numbers[i];

                                                a13Count++;

                                }

                                if(numbers[i]%2==0){

                                                //even number

                                                Aeven[aEvenCount]=numbers[i];

                                                aEvenCount++;

                                }

                }

                //defining an output file

                ofstream outFile("outputFile.txt");

                //displaying each details into the screen as well as appending to the output file

                cout<<"Original array:"<<endl;//displaying on screen

                outFile<<"Original array:"<<endl;//printing to file

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

                                cout<<numbers[i]<<" ";

                                outFile<<numbers[i]<<" ";

                }

                cout<<" Count: "<<count<<endl;

                outFile<<" Count: "<<count<<endl;

                cout<<"A13:"<<endl;

                outFile<<"A13:"<<endl;

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

                                cout<<A13[i]<<" ";

                                outFile<<A13[i]<<" ";

                }

                cout<<" Count: "<<a13Count<<endl;

                outFile<<" Count: "<<a13Count<<endl;

                cout<<"Aeven:"<<endl;

                outFile<<"Aeven:"<<endl;

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

                                cout<<Aeven[i]<<" ";

                                outFile<<Aeven[i]<<" ";

                }

                cout<<" Count: "<<aEvenCount<<endl;

                outFile<<" Count: "<<aEvenCount<<endl;

                outFile.close();

               

                return 0;

}

//integers.txt

1

2

4

22

44

18

15

12

36

88

27

13

26

45

3

6

8

//output and outputFile.txt

Original array:

1 2 4 22 44 18 15 12 36 88 27 13 26 45 3 6 8

Count: 17

A13:

13 26

Count: 2

Aeven:

2 4 22 44 18 12 36 88 26 6 8

Count: 11

//program2.cpp

#include<iostream>

#include<fstream>

using namespace std;

/*

* Program to read a file of course names, store them into an array of c strings,

* and display them on the screen

*/

int main(){

                //defining c string array to store course names

                char courseNames[10][50];

                //total courses counter

                int count=0;

                //initializing input file

                ifstream inFile("courses.txt");

                if(!inFile){

                                cout<<"Cannot open input file";

                                return 1;

                }

                char discard;//used to discard the new line character

                //reading each and every course names, line by line

                while(inFile.get(courseNames[count],50)){

                                count++;

                                //discarding newline character

                                inFile.get(discard);

                               

                }

                //displaying courses

                cout<<"COURSES READ:"<<endl;

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

                                cout<<courseNames[i]<<endl;

                }

               

                return 0;

}

//courses.txt

English

Computer Science

Humanities

Maths

Physics

Biology

Radiology

Engineering

Social Science

/*OUTPUT*/

COURSES READ:

English

Computer Science

Humanities

Maths

Physics

Biology

Radiology

Engineering

Social Science