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

Below I have answer the questions I know however I 2. Define a struct , fruitTyp

ID: 3537512 • Letter: B

Question

Below I have answer the questions I know however I

2.      Define a struct, fruitType to store the following data about a fruit: fruit name (string), color (string), calories (int), sugar (int), dietary fiber(int) and carbohydrate(int). 1 point


3.      Using the struct defined in previous question, declare a variable named fruit of the fruitType to store the following data: fruit name %u2013 Apple, color %u2013 Red, calories %u2013 130, sugar %u2013 25, dietary fiber - 5 and carbohydrate - 34. 1 point


4.      Use the struct fruitType defined in question 2.

a.      Declare an array, fruit, of 4 components of the type, fruitType. 1 point

fruitType.fruit[4];

b.     Write the C++ code( use a loop) to initialize each component of fruit as follows: fruit name to null string, color to null string, calories to 0, sugar to 0, dietary fiber to 0 and carbohydrate to 0. 2 points

c.      Write a C++ code that uses a loop to output the data stored in fruit. 2 points

Programming Exercise   6 points

1.      Create a C++ program using the fruitType struct definition given above. Label the file as fruit.cpp.

2.      Include the cstdlib header file and add a system("Pause") ; in your program. [This is to make sure our standalone .exe file works without IDE.]

3.      Create an array, fruit of 4 components of the fruitType. Write the C++ code( use a loop) to initialize each component of fruit as follows: fruit name to null string, color to null string, calories to 0, sugar to 0, dietary fiber to 0 and carbohydrate to 0

4.      Write a C++ code that uses a loop to output the data stored in fruit. (See sample run 1)

  fruit.dat file is shown with tabs as delimiting character

5.      After the following steps are executed, open the file labeled fruit.dat (available in week 10 folder) and read data into the fruit array (use a for loop %u2013 be sure to use getline method to read the fruit name and color and include code to ignore the newline character after reading each line). In the fruit.dat file, tabs are used as delimiting character.

6.      Then use a for loop to print the values in fruit array (See Sample run 2).


7.      Quit the program after testing; close Dev C++. Navigate to your week 10 folder and double click the fruit.exe file. This tests if your standalone program (fruit.exe) works in full and has a message "Press any key to continue".

Explanation / Answer

2. Define a struct, fruitType to store the following data about a fruit:
fruit name (string), color (string), calories (int), sugar (int), dietary fiber(int) and carbohydrate(int). 1 point

struct fruitType
{
string fruit_name;
string color;
int calories;
int sugar;
int dietary_fiber;
int carbohydrate;
};

3. Using the struct defined in previous question, declare a variable named fruit of the fruitType to store the following data:
fruit name %u2013 Apple, color %u2013 Red, calories %u2013 130, sugar %u2013 25, dietary fiber - 5 and carbohydrate - 34. 1 point

struct fruitType fruit;
fruit.fruit_name="Apple";
fruit.color="Red";
fruit.calories=130;
fruit.sugar=25;
fruit.dietary_fiber=5;
ifruit.carbohydrate=34;


4.Use the struct fruitType defined in question 2.

a. Declare an array, fruit, of 4 components of the type, fruitType. 1 point

struct fruitType fruit[4];

b.     Write the C++ code( use a loop) to initialize each component of fruit as follows:
fruit name to null string, color to null string, calories to 0, sugar to 0, dietary fiber to 0 and carbohydrate to 0. 2 points

for(int i=0; i<4; i++)
{
fruit[i].fruit_name="";
fruit[i].color="";
fruit[i].calories=0;
fruit[i].sugar=0;
fruit[i].dietary_fiber=0;
ifruit[i].carbohydrate=0;
}

c.      Write a C++ code that uses a loop to output the data stored in fruit. 2 points

cout << " fruit name" << fruit.fruit_name << "fruit_color" << fruit.color << "calories " << fruit.calories << "sugar" << fruit.sugar << "dietary_fiber" << fruit.dietary_fiber << "carbohydrate" << fruit.carbohydrate << endl;



// PROGRAM IS


#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct fruitType
{
string fruit_name;
string color;
int calories;
int sugar;
int dietary_fiber;
int carbohydrate;
};

int main()
{
ifstream infile("fruit.dat");
struct fruitType fruit[4];
for(int i=0; i<4; i++)
{
fruit[i].fruit_name="";
fruit[i].color="";
fruit[i].calories=0;
fruit[i].sugar=0;
fruit[i].dietary_fiber=0;
fruit[i].carbohydrate=0;
}
for(int i=0; i<4; i++)
{
    std::cout << fruit[i].fruit_name << " " << fruit[i].color << " " << fruit[i].calories << " " << fruit[i].sugar << " "<< fruit[i].dietary_fiber << " " << fruit[i].carbohydrate << endl;
}
for(int i=0; i<4; i++)
{
infile >> fruit[i].fruit_name >> fruit[i].color >> fruit[i].calories >> fruit[i].sugar >> fruit[i].dietary_fiber >> fruit[i].carbohydrate;
}
for(int i=0; i<4; i++)
{
cout << fruit[i].fruit_name << " " << fruit[i].color << " " << fruit[i].calories << " " << fruit[i].sugar << " "<< fruit[i].dietary_fiber << " " << fruit[i].carbohydrate << endl;
}
system("pause");
    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote