Question

In: Computer Science

C++ PLEASE Write a new program called Lab15A Write a void function named fillArray that receives...

C++ PLEASE

  1. Write a new program called Lab15A

    1. Write a void function named fillArray that receives an array of 10 integers as a parameter and fills the array by reading the values from a text file (Lab15A.txt).
      It should also print the values of the array all on the same line, separated by spaces.

    2. Write an int function named findLast that receives an array of 10 integers as a parameter and returns the last negative number in the array.

    3. Write an int function named findSmallest that receives an array of 10 integers as a parameter and returns the smallest number in the array.

    4. In your main method

      1. Declare the array of 10 integers

      2. Call fillArray, sending the array as a parameter.

      3. Call findLast, sending the array as a parameter, and print the value it returns with a label.

      4. Call findSmallest, sending the array as a parameter, and print the value it returns with a label.

Solutions

Expert Solution

I have implemented the following methods ::

1> void fillArray(int arr[]) : This function store integers into array from the "Lab15A.txt" file.

2> int findLast(int arr[]) : This function returns last negative number from the array otherwise return -1.

3> int findSmallest(int arr[]) : This function return smallest element from the array.

Here, #include<fstream> header file used to read integers from the "Lab15A.txt" file.

Program:-

#include<iostream>
#include<fstream>

using namespace std;

// This function store integers from txt file
void fillArray(int arr[]){

   ifstream in("Lab15A.txt");
   int cnt= 0;
   int x;
   // check that array is not already full
   while (cnt < 10 && in >> x){
       // and read integer from file
       arr[cnt++] = x;
   }

   // print the integers stored in the array
   cout<<"From filledArray() Array Values :"<<"\n";
   for (int i = 0; i < cnt; i++) {
      cout << arr[i] <<' ';
   }

   //close the file
   in.close();
}


// This function returns last negative numbers otherwise returns -1.
int findLast(int arr[]){

    int getLastNegative = -1;
    bool found = false;
    for (int i = 9; i >= 0; i++) {
        if(arr[i] < 0){
            //cout<<arr[i];
            getLastNegative = arr[i];
            found = true;
            break;
        }

    }

    if(found)
        return getLastNegative;
    else
        return getLastNegative;
}


// This function find smallest from the array
int findSmallest(int arr[]){

    int min = arr[0];

    for(int i=1; i<10; i++){

        if(arr[i] < min)
            min = arr[i];

    }

    return min;

}

int main(){
    int arr[10];

    // call the fillArray()
    fillArray(arr);

    // call the findLast()
    cout<<"\n\nLast Negative element from array : "<<findLast(arr);

    // call findSmallest()
    cout<<"\n\nFind Smallest : "<<findSmallest(arr);
    return 0;
}

Output:-

I hope you will understand the above program.

Do you feel needful and useful then please upvote me.

Thank you.


Related Solutions

Using c++, write a program that will display your name as a void function then will...
Using c++, write a program that will display your name as a void function then will perform the following by user-defined functions: a. to compute for the sum of two numbers (n1, n2) using function.
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
Write a Python program: The function is named validity(). It receives 2 floating point parameters, min...
Write a Python program: The function is named validity(). It receives 2 floating point parameters, min and max, from the program that invoked it. The function asks the user to enter a float number. Using a while loop it checks whether the number is valid (between min and max, inclusive). If not valid, the while loop uses this statement to prompt for a new number: num = float (input (" Enter a number that is at least :"+ str(min) +...
Write C++ void function called averageTemperature() that asks the user to enter integers with a sentinel...
Write C++ void function called averageTemperature() that asks the user to enter integers with a sentinel value of -999 to stop entering numbers. Then average the numbers and print the average. You will need an int acculumator for the temperatures and an int acculumator for the number of temperatures entered. Use the accumulators to calculate the average which will need to be a decimal number. Call the function from the main() function.
Python program.  from random import . Write a function called cleanLowerWord that receives a string as a...
Python program.  from random import . Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks...
a. Write a c++ function called IsPalindrome that receives a 3-digit number and returns whether it...
a. Write a c++ function called IsPalindrome that receives a 3-digit number and returns whether it is palindrome or not. [2.5 marks] b. Write a c++ function, called GCD that receives two parameters n, d, and prints their greatest common divisor [2.5 marks] Now after you created the two functions, you have to write a main function that can call the above functions according to user choice. Write a menu that allows the user to select from the following options...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is positive, the function will assign it the next leap year after it; otherwise, the function will assign 4 to it.
USING C++ PLEASE Write a program named Lab11C that calculates the average of a group of...
USING C++ PLEASE Write a program named Lab11C that calculates the average of a group of test scores where the lowest score is dropped. It should use the following functions a) void getScores should have 4 double reference parameters (one for each test score) getScores should do the following: - read 4 test scores from the text file Lab11C.txt (Important: declare your ifstream infile and open the file inside this function, not in the main function) - store them in...
"sum_between" function Write a function named "sum_between" that receives 2 parameters - "start" (an int) and...
"sum_between" function Write a function named "sum_between" that receives 2 parameters - "start" (an int) and "end" (an int). It should return the sum (total) of all of the integers between (and including) "start" and "end". If "end" is less than "start", the function should return -1 instead. e.g. if you give the function a start of 10 and an end of 15, it should return 75 (i.e. 10+11+12+13+14+15)
C++ Write the C++ code for a void function that prompts the user to enter a...
C++ Write the C++ code for a void function that prompts the user to enter a name, and then stores the user's response in the string variable whose address is passed to the function. Name the function getName.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT