CREATE TWO C++ PROGRAMS :
Program 1
Write a program that reads in babynames.txt (provided) and outputs the top 20 names, regardless of gender.
The file has the following syntax:
RANK# BoyName Boy#OfBirths Boy% GirlName Girl#OfBirths Girl%
You should ignore the rank and percentages. Compare the number of births to rerank.
Output should go to standard out.
Program 2
Write a program that reads a text file containing floating-point numbers. Allow the user to specify the source file name on the command-line. If the user doesn’t specify any file name, then prompt the user for the file name (cin).
Compute the average of the numbers in the file and output it to a file named “results.txt”.
Example output
Program 1 (not test output, however, will look like this)
1. John 2. Jane 3. Mary
Program 2
Note: program will have no output to standard in when a command line argument is provided.
Source filename: SSSS
Contents of “results.txt” afterwards:
3412.2345 FOR PROGRAM 1 - how the text file babynames.txt looks: 1 Michael 462085 2.2506 Jessica 302962 1.5436 2 Christopher 361250 1.7595 Ashley 301702 1.5372 3 Matthew 351477 1.7119 Emily 237133 1.2082 4 Joshua 328955 1.6022 Sarah 224000 1.1413 5 Jacob 298016 1.4515 Samantha 223913 1.1408 6 Nicholas 275222 1.3405 Amanda 190901 0.9726 7 Andrew 272600 1.3277 Brittany 190779 0.9720 8 Daniel 271734 1.3235 Elizabeth 172383 0.8783 9 Tyler 262218 1.2771 Taylor 168977 0.8609 10 Joseph 260365 1.2681 Megan 160312 0.8168 11 Brandon 259299 1.2629 Hannah 158647 0.8083 12 David 253193 1.2332 Kayla 155844 0.7940 13 James 244775 1.1922 Lauren 153530 0.7822 14 Ryan 241105 1.1743 Stephanie 149725 0.7628 15 John 239730 1.1676 Rachel 148907 0.7587 16 Zachary 225188 1.0968 Jennifer 147948 0.7538 17 Justin 220012 1.0716 Nicole 136033 0.6931 18 William 217588 1.0598 Alexis 131117 0.6680 19 Anthony 216088 1.0525 Victoria 117386 0.5981 20 Robert 205313 1.0000 Amber 115551 0.5887 21 Jonathan 197250 0.9607 Alyssa 115213 0.5870 22 Austin 190053 0.9257 Courtney 113257 0.5770 23 Alexander 183797 0.8952 Danielle 109541 0.5581 24 Kyle 180151 0.8774 Rebecca 109514 0.5580 25 Kevin 165085 0.8041 Jasmine 105247 0.5362 26 Thomas 146515 0.7136 Brianna 99046 0.5046 27 Cody 146426 0.7132 Katherine 96812 0.4933 28 Jordan 144661 0.7046 Alexandra 94743 0.4827 29 Eric 135145 0.6582 Madison 91870 0.4681 30 Benjamin 133718 0.6513 Morgan 91162 0.4645 31 Aaron 128202 0.6244 Melissa 89932 0.4582 32 Christian 125899 0.6132 Michelle 86458 0.4405 33 Samuel 125196 0.6098 Kelsey 85120 0.4337 34 Dylan 122008 0.5942 Chelsea 84781 0.4320 35 Steven 121009 0.5894 Anna 78320 0.3990 36 Brian 119216 0.5806 Kimberly 77846 0.3966 37 Jose 118843 0.5788 Tiffany 77493 0.3948 38 Timothy 107680 0.5245 Olivia 75854 0.3865 39 Nathan 107229 0.5223 Mary 75247 0.3834 40 Adam 106822 0.5203 Christina 74601 0.3801 41 Richard 94805 0.4618 Allison 74398 0.3791 42 Patrick 92874 0.4523 Abigail 72691 0.3704 43 Charles 92372 0.4499 Sara 72064 0.3672 44 Sean 91953 0.4479 Shelby 71974 0.3667 45 Jason 91622 0.4462 Heather 71603 0.3648 46 Cameron 81097 0.3950 Haley 71168 0.3626 47 Jeremy 77749 0.3787 Maria 70741 0.3604 48 Mark 75751 0.3689 Kaitlyn 67544 0.3441 49 Stephen 75064 0.3656 Laura 67109 0.3419 50 Jesse 73701 0.3590 Erin 65312 0.3328 51 Juan 70809 0.3449 Andrea 65183 0.3321 52 Alex 70772 0.3447 Natalie 63348 0.3228 53 Travis 68176 0.3321 Jordan 61736 0.3145 54 Jeffrey 66961 0.3261 Brooke 60614 0.3088 55 Ethan 66878 0.3257 Julia 59899 0.3052 56 Caleb 66212 0.3225 Emma 57961 0.2953 57 Luis 64656 0.3149 Vanessa 57868 0.2948 58 Jared 63748 0.3105 Erica 55740 0.2840 59 Logan 63541 0.3095 Sydney 55737 0.2840 60 Hunter 60499 0.2947 Kelly 55710 0.2838 61 Trevor 59912 0.2918 Kristen 53066 0.2704 62 Bryan 59704 0.2908 Katelyn 52795 0.2690 63 Evan 59366 0.2891 Marissa 51191 0.2608 64 Paul 58833 0.2865 Amy 51141 0.2606 65 Taylor 58384 0.2844 Crystal 51055 0.2601 66 Kenneth 58223 0.2836 Paige 50539 0.2575 67 Connor 57363 0.2794 Cassandra 50342 0.2565 68 Dustin 56742 0.2764 Gabrielle 49787 0.2537 69 Noah 56093 0.2732 Katie 49168 0.2505 70 Carlos 55095 0.2683 Caitlin 49011 0.2497 71 Devin 55039 0.2681 Lindsey 48968 0.2495 72 Gabriel 54756 0.2667 Destiny 48199 0.2456 73 Ian 53974 0.2629 Kathryn 47111 0.2400 74 Nathaniel 52541 0.2559 Jacqueline 46051 0.2346 75 Gregory 52063 0.2536 Shannon 45900 0.2339 76 Derek 50732 0.2471 Jenna 45839 0.2335 77 Corey 50201 0.2445 Angela 45134 0.2300 78 Jesus 49036 0.2388 Savannah 44685 0.2277 79 Scott 48999 0.2387 Mariah 44570 0.2271 80 Bradley 48063 0.2341 Alexandria 44194 0.2252 81 Dakota 46628 0.2271 Sierra 43603 0.2222 82 Antonio 46507 0.2265 Alicia 43463 0.2214 83 Marcus 46434 0.2262 Briana 42781 0.2180 84 Blake 45846 0.2233 Miranda 42207 0.2150 85 Garrett 45305 0.2207 Jamie 41425 0.2111 86 Edward 45098 0.2197 Catherine 40718 0.2075 87 Luke 44381 0.2162 Brittney 40018 0.2039 88 Shawn 42450 0.2068 Breanna 40011 0.2039 89 Peter 42317 0.2061 Grace 39814 0.2029 90 Seth 42176 0.2054 Monica 38866 0.1980 91 Mitchell 41997 0.2045 Sabrina 38638 0.1969 92 Adrian 41952 0.2043 Madeline 36949 0.1883 93 Victor 41016 0.1998 Caroline 36789 0.1874 94 Miguel 40947 0.1994 Molly 36540 0.1862 95 Shane 40577 0.1976 Erika 35055 0.1786 96 Chase 40287 0.1962 Mackenzie 34769 0.1771 97 Isaac 40183 0.1957 Leah 34040 0.1734 98 Spencer 38164 0.1859 Diana 33747 0.1719 99 Lucas 38141 0.1858 Whitney 32877 0.1675 100 Jack 37456 0.1824 Cheyenne 31825 0.1621
Program 1:
#include <iostream> #include <string> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Baby{ string name; int births; }; //Comparator function for STL's sort() function bool comparator(Baby b1, Baby b2){ return (b1.births > b2.births); } int main(){ vector<Baby> babies; ifstream in("babynames.txt");//Open a file to read if(!in){ cout << "Error: Cannot open file" << endl; exit(EXIT_FAILURE); } string name; int rank, births; float percentage; Baby b; //Read from file while(!in.eof()){ //Read boy's data in >> rank >> name >> births >> percentage; b.name = name; b.births = births; babies.push_back(b); //Read girl's data in >> name >> births >> percentage; b.name = name; b.births = births; babies.push_back(b); } in.close();//Close file //Sort babies vector in descending order sort(babies.begin(), babies.end(), comparator); //Print baby names with new ranks for(int i = 0; i < babies.size(); i++){ cout << i + 1 << ". " << babies[i].name << endl; } return 0; }
Output:
Program 2:
#include <iostream> #include <string> #include <fstream> using namespace std; int main(int argc, char **argv){ string filename; if(argc == 1){ cout << "Enter input file name: "; cin >> filename; } else{ filename = argv[1]; } ifstream in(filename);//Open a file to read if(!in){ cout << "Error: Cannot open file" << endl; exit(EXIT_FAILURE); } int count = 0; float num, avg = 0.0f; //Read from file while(!in.eof()){ in >> num; count++; avg += num; } in.close();//Close file avg = avg/count; ofstream out("results.txt");//Open file to output result out << avg; out.close();//Close file return 0; }
File input:
Command line output:
File output:
CREATE TWO C++ PROGRAMS : Program 1 Write a program that reads in babynames.txt (provided) and...
//Done in C please! //Any help appreciated! Write two programs to write and read from file the age and first and last names of people. The programs should work as follows: 1. The first program reads strings containing first and last names and saves them in a text file (this should be done with the fprintf function). The program should take the name of the file as a command line argument. The loop ends when the user enters 0, the...
Write a program in Java according to the following specifications: The program reads a text file with student records (first name, last name and grade on each line). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "print" - prints the student records (first name, last name, grade). "sortfirst" - sorts the student records by first name. "sortlast" - sorts the student records by last name. "sortgrade" - sorts the...
Following should be done in C++: Create a program that reads a text file and prints out the contents of the file but with all letters converted to uppercase. The name of the file to be read by the program will be provided by the user. Here are some example session: Contents of "data.txt": Hello, World! User input: data.txt Program output: HELLO, WORLD! Contents of "data.txt": tHiS FiLe HaS mIxEd CaSeS User input: data.txt Program output: THIS FILE HAS MIXED...
( IN JAVA): Write a program that reads each line in a file, and writes them out in reverse order into another file. This program should ask the user for the name of an input file, and the name of the output file. If the input file contains: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go Then the output file should end up containing: The lamb...
How can I create Loops and Files on Java? • Create a program that reads a list of names from a source file and writes those names to a CSV file. The source file name and target CSV file name should be requested from the user • The source file can have a variable number of names so your program should be dynamic enough to read as many names as needed • When writing your CSV file, the first row...
In c programming, I need to write a program that reverses each of the lines in a file. The program accepts two command line arguments: The first one is the name of the input file The second is the name of the output file If the user didn't give two filenames, display an error message and exit. The program reads in each line of the input file and writes each line in reverse to the output file. Note, the lines...
This is for a class in C++ Problem 1: A class Names reads the following names from the .txt file Name.txt [Insert a long list of first, middle and last names. Each set of names is placed on a different line like the following: John Doe Jane Doe Johnie Boy etc.] Make a search directory using dynamic allocation (pointers) where the user would search the any number of letters of the first name and the matching name/names would come up...
C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...
C Language program problem. Suppose that I want to create a program by using command line. User will type a name of file such as "test.data" to access the content of the file. Program should scan whole file and create a list with 30 position . You should show the two situation, one is for when scanned space, the other one is when it scanned new line. Then the other task is printing them out. You see there are spaces...
Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...