Question

In: Computer Science

Write three functions that will print your names 25 times to csis.txt and the console. One...

Write three functions that will print your names 25 times to csis.txt and the console. One function will use a  for() loop, one function  a while() loop and the final function will use a do while() loop.

Here are the three prototypes for the functions:

void nameUsingForLoop(int);

void nameUsingWhileLoop(int);

void nameUsingDoWhileLoop(int);

Call each function in main() using an input parameter 25.

Each function has one int parameter, example:

void nameForLoop(int times){

}

submit nameLoop.c and csis.txt files

use the statements in the loop to give yourself 5 columns and 5 rows:

if(count % 5 == 0){

printf(“\n”);

}

use braces {  } around both statements:

if(count % 5 == 0){  

     printf("\n");  

  fprintf(fp,"\n");  

}

write functions:

//prototype void use_while_loop(void);

int main(){

//call use_while_loop();

Solutions

Expert Solution

Below is the code in C++ containing three functions for different loop methods as mentioned in the question. The program is able to print on the console and on the file simultaneously as mentioned. Sample output is attached at the end for the same.

Note:- Change "xyz" to your name.

#include <iostream>
#include <stdlib.h>
using namespace std;

// function to implement the task using for loop.
void nameForLoop(int times){
    // creating file pointer.
    FILE *fp;
    // opening the file csis.txt to write names on to it.
    fp = fopen ("csis.txt", "w+");
    for(int i = 1; i<=25; i++){
        if(i % 5 == 0){
            printf("xyz ");
            printf("\n");
        }

        if(i % 5 == 0){  
            fprintf(fp,"xyz ");
            fprintf(fp,"\n");
        }
        
        if(i%5 != 0){
            printf("xyz ");
            fprintf(fp,"xyz ");
        }
    }
    fclose(fp);
}

// function to implement the task using while loop.
void nameWhileLoop(int times){
    // creating file pointer.
    FILE *fp;
    // opening the file csis.txt to write names on to it.
    fp = fopen ("csis.txt", "w+");
    int i = 1;
    while(i <= 25){
        if(i % 5 == 0){
            printf("xyz ");
            printf("\n");
        }

        if(i % 5 == 0){  
            fprintf(fp,"xyz ");
            fprintf(fp,"\n");
        }
        
        if(i%5 != 0){
            printf("xyz ");
            fprintf(fp,"xyz ");
        }
        i++;
    }
    fclose(fp);
}

// function to implement the task using doWhile loop.
void nameDoWhileLoop(int times){
    // creating file pointer.
    FILE *fp;
    // opening the file csis.txt to write names on to it.
    fp = fopen ("csis.txt", "w+");
    int i = 1;
    do {
        if(i % 5 == 0){
            printf("xyz ");
            printf("\n");
        }

        if(i % 5 == 0){  
            fprintf(fp,"xyz ");
            fprintf(fp,"\n");
        }
        
        if(i%5 != 0){
            printf("xyz ");
            fprintf(fp,"xyz ");
        }
        i++;
    } while(i <= 25);
    fclose(fp);
}

int main()
{
    // uncomment the function you want to execute. By default doWhile will run.
     
    //nameForLoop(25);
    //nameWhileLoop(25);
    nameDoWhileLoop(25);
    return 0;
}

Sample Output on file:-

Sample Output on console:-


Related Solutions

Please write the following swap functions and print code in main to show that the functions...
Please write the following swap functions and print code in main to show that the functions have adequately . Your code should, in main(), print the values prior to being sent to the swap function. In the swap function, the values should be swapped and then in main(), please print the newly swapped values. 1) swap integer values using reference parameters 2) swap integer values using pointer parameters 3) swap pointers to integers - you need to print the addresses,...
1.Suppose a dictionary Groceries maps items to quantities.     Write a python to print the names...
1.Suppose a dictionary Groceries maps items to quantities.     Write a python to print the names of the grocery items whose quantity exceeds 400.     For example, if groceries = {‘cake mixes’: 430, ‘cheese’:312, ‘Orange’:525, ‘candy bars’: 217}         The output will be                                 Cake mixes                                 Orange 2. which Python string method would be the best choice to print the number of time “the” occurs in a sentence entered by the user? Provide a one word answer. Just...
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the reverse order. Using a ListItreator output the contents of the LinkedList in the original order.
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the original order. Using a ListItreator output the contents of the LinkedList in the reverse order.
Write a program names EncryptDecrypt.java that has the following menu choices: Print menu and allow user...
Write a program names EncryptDecrypt.java that has the following menu choices: Print menu and allow user to choose options. The program must have a file dialogue box for text file. Output should be based on user choices. Read in a file Print the file to the console Encrypt the file and write it to the console Write out the encrypted file to a text file Clear the data in memory Read in an encrypted file Decrypt the file Write out...
Write a java project that reads a sequence of up to 25 pairs of names and...
Write a java project that reads a sequence of up to 25 pairs of names and postal (ZIP) codes for individuals (sample input data is attached). Store the data in an object designed to store a first name (string), last name (string), and postal code (integer). Assume each line of input will contain two strings followed by an integer value, each separated by a tab character. Then, after the input has been read in, print the list in an appropriate...
Write in C++ Write a program that accepts the names of three political parties and the...
Write in C++ Write a program that accepts the names of three political parties and the number of votes each received in the last mayoral election. Display the percentage of the vote each party received.   Be sure to provide labels (party name) and number-align your output values.
# List at least three functions in the linked list toolbox, including function names and their...
# List at least three functions in the linked list toolbox, including function names and their functionality. # Assuming we have already implemented all functions in the linked list toolbox and we have a pointer head_ptr pointing to the front of a linked list. Write a few lines to insert the number 42 and make it the second item in the list. Note: if the list was originally empty, then it should be the first in the list.
Write a program in C++ Write three new functions, mean() and standard_deviation(). Modify your code to...
Write a program in C++ Write three new functions, mean() and standard_deviation(). Modify your code to call these functions instead of the inline code in your main(). In addition, add error checking for the user input. If the user inputs an incorrect value prompt the user to re-enter the number. The flow of the code should look something like: /** Calculate the mean of a vector of floating point numbers **/ float mean(vector& values) /** Calculate the standard deviation from...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The function will have one int 1D array n and one int size as parameters. The function will display all the values of n on one line. Function #2 Write the function AverageEvenOdd. The function will have one int 1D array n and one int size as parameters. The size of the array is given by the parameter int size. Therefore, the function should work...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT