Question

In: Computer Science

Create a new project called Lab05b. Keep the main file named main. Write a function readAndPrint,...

  • Create a new project called Lab05b. Keep the main file named main.
  • Write a function readAndPrint, and power with parameters noted below in the Functions.h file.

//Your Last Name

#ifndef FUNCTIONS_H

#define FUNCTIONS_H

#include <fstream>

#include <iostream>

#include <string>

using namespace std;

/*reads from a file

and prints every item to the screen*/

// file contains sets of

//int

//string

//Don't forget to use infile.ignore() between << and getline

//ifstream must be passed by reference

void readAndPrint(ifstream &infile);

//uses a for loop to return the base to the exponent

//e.g. 2^4 = 16

// this fuction should read data.txt file

long power(long base, long exponent);

#endif

  • You should place the function prototypes in a file called Functions.h The function prototype has a semi-colon at the end.
  • Then you should implement in a file called Functions.cpp
  • Finally, declare the infile, and call the readAndPrint function. Then look at the power of 2^3 and 10 ^ 4

  • Create a file named data.txt that has the following information.

1

Alpha

2

Beta

3

Gamma

  • Your functions.cpp AND your lastnamefirstLab05b.cpp should #include “Functions.h”.
  • Remember that you should not #include.cpp files!
  • Implementation detail 4: compile and run with:

Solutions

Expert Solution

If you have any doubts, please give me comment...

Functions.h

#ifndef FUNCTIONS_H

#define FUNCTIONS_H

#include <fstream>

#include <iostream>

#include <string>

using namespace std;

/*reads from a file

and prints every item to the screen*/

// file contains sets of

//int

//string

//Don't forget to use infile.ignore() between << and getline

//ifstream must be passed by reference

void readAndPrint(ifstream &infile);

//uses a for loop to return the base to the exponent

//e.g. 2^4 = 16

// this fuction should read data.txt file

long power(long base, long exponent);

#endif

Functions.cpp

#include "Functions.h"

void readAndPrint(ifstream &infile){

    infile.open("data.txt");

    int num;

    string str;

    while(!infile.eof()){

        infile>>num;

        infile.ignore();

        getline(infile, str);

        cout<<num<<endl;

        cout<<str<<endl;

    }

    infile.close();

}

long power(long base, long exponent){

    int result = 1;

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

        result *= base;

    }

    return result;

}

ANLab0b.cpp

#include<iostream>

#include "Functions.h"

int main(){

    ifstream infile;

    readAndPrint(infile);

    cout<<endl;

    cout<<"2^3 = "<<power(2, 3)<<endl;

    cout<<"2^4 = "<<power(2, 4)<<endl;

    cout<<"10^4 = "<<power(10, 4)<<endl;

    return 0;

}


Related Solutions

Start NetBeans. Create a new project called Lab7. Create a Java main class file using the...
Start NetBeans. Create a new project called Lab7. Create a Java main class file using the class name YourlastnameLab7 with your actual last name. Create a Java class file for a Polygon class. Implement the Polygon class. Add a private instance variable representing the number of sides in the polygon. Add a constructor that takes a single argument and uses it to initialize the number of sides. If the value of the argument is less than three, display an error...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
​​​​Python Create a new file named compute_cost.py. Write your name and date in a comment. Create...
​​​​Python Create a new file named compute_cost.py. Write your name and date in a comment. Create a variable named base_fee and set the value to 5.5. Prompt the user for the zone. The zones can be an integer value from 1 to any value for the zone. Convert the zone to an integer. Display the zone on the SenseHat with a scroll speed of 0.3, make the text blue, and the background yellow. Scroll "The zone is " and the...
open up a new Java project on Eclipse named Review and create a new class called...
open up a new Java project on Eclipse named Review and create a new class called Review.java. Copy and paste the below starter code into your file: /** * @author * @author * CIS 36B */ //write your two import statements here public class Review {        public static void main(String[] args) { //don't forget IOException         File infile = new File("scores.txt");         //declare scores array         //Use a for or while loop to read in...
Create a Visual Studio console project named exercise101. The main() function should prompt for the name...
Create a Visual Studio console project named exercise101. The main() function should prompt for the name of a text file located in the same directory as exercise101.cpp, and search for a word in the text file as follows: Enter text file name: Enter search word: The program should print the number of occurrences of the word in the file: occurrences of were found in If the file could not be opened then display: File not found Use Stream file I/O...
USING JAVASCRIPT Create a file name dayOfWeek.js and write an arrow function named dayOfWeek that accepts...
USING JAVASCRIPT Create a file name dayOfWeek.js and write an arrow function named dayOfWeek that accepts a Date object dateStr and returns a string that is the day of the week in English form (i.e. “Sunday”, “Monday”, etc.). Test your function by creating a date object that is a significant date to you (such as your birthday) and passing that date object to your function. Test your function at least twice with two different dates. Submit the dayOfWeek.js file to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT