C++ Please Contact list - functions & parallel arrays/CStrings
No Vectors can be used since we haven't learned them yet !!
A contact list is a place where you can store a specific information with other associated information such as a phone number, email address, birthday, etc. Write a program that will read 5 data pairs into two parallel arrays. Data pairs consist of a name (as a CString) and a GPA (double). That list is followed by a name, and your program should call a function that will search the name array and return the corresponding gpa that will be output from main(). If the name is not in the array, the function should return -1.
A single name would be stored as a CString (character array). Because we are storing multiple names, we will use a 2-dimensional array. You may assume names will not be longer than 15 characters.
| J | o | e | \0| | | | L | i | n | d | a | \0| | F | r | a | n | k | \0| | S | a | l | l | y | \0|
Ex: If the input is:
Joe 3.7 Linda 2.8 Frank 3.0 Mark 2.5 Sally 4.0 Frank
the output is:
3.0
Your program must
define and call the following function. The return value of GetGPA
is the gpa associated with the target name.
double GetGPA(char names[ ][ ], double gpas[ ], char
targetName[ ])
Hint: Use two separate arrays: One for the CString names, and the other for the gpas.
#include <iostream>
#include
<cstring> //
Cstring functions
#include
<iomanip> //
format output
using namespace std;
// array size constants
const int NAME_SIZE = 15;
const int SIZE = 5;
/* Define your function here */
int main() {
// declare arrays
char names[SIZE][NAME_SIZE];
double gpas[SIZE];
/* Type your code here */
return 0;
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#include <iostream>
#include <cstring> // Cstring functions
#include <iomanip> // format output
using namespace std;
// array size constants
const int NAME_SIZE = 15;
const int SIZE = 5;
/* required function */
double GetGPA(char names[SIZE][NAME_SIZE], double gpas[], char targetName[]){
//looping and searching for targetName in names
for(int i=0;i<SIZE;i++){
if(strcasecmp(names[i],targetName)==0){
//found, returning the gpa
return gpas[i];
}
}
//not found
return -1;
}
int main() {
// declare arrays
char names[SIZE][NAME_SIZE];
double gpas[SIZE];
/* looping and reading names and gpas */
for(int i=0;i<SIZE;i++){
cin>>names[i];
cin>>gpas[i];
}
//declaring a char array and reading target name
char targetName[NAME_SIZE];
cin>>targetName;
//finding gpa
double gpa=GetGPA(names,gpas,targetName);
//setting a fixed precision of 1 digit
cout<<setprecision(1)<<fixed;
//if gpa returned is -1, printing 'Not found', or if you want to display -1
//, just cout the gpa as it is.
if(gpa==-1){
//not found
cout<<"Not found!"<<endl;
}else{
//found,displaying gpa
cout<<gpa<<endl;
}
return 0;
}
/*OUTPUT*/
Joe 3.7 Linda 2.8 Frank 3.0 Mark 2.5 Sally 4.0
Frank
3.0
C++ Please Contact list - functions & parallel arrays/CStrings No Vectors can be used since we...
C++ Please Adjust list by normalizing - functions & arrays No Vectors can be used since we haven't learned them yet !! When analyzing data sets, such as data for human heights or for human weights, a common step is to adjust the data. This can be done by normalizing to values between 0 and 1, or throwing away outliers. For this program, adjust the values by subtracting the smallest value from all the values. The input begins with an...
A contact list is a place where you can store a specific contact with other associated information such as a phone number, email address, birthday, etc. Write a program that first takes as input an integer N that represents the number of word pairs in the list to follow. Word pairs consist of a name and a phone number (both strings). That list is followed by a name, and your program should output the phone number associated with that name....
A hard c++ problem ◎Write a c++ program”Student.h”include Private data member, string firstName; string lastName; double GPA(=(4.0*NumberOfAs+3.0*NumberOfBs+2.0*NumberOfCs+1.0*NumberOfDs)/( As+Bs+Cs+Ds+Fs)); Public data member, void setFirstName(string name); string getFirstName() const; void printFirstName() const; void getLastName(string name); string getLastName() const; void printLastName() const; void computeGPA(int NumberOfAs,int NumberOfBs,int NumberOfCs,int NumberOfDs,int NumberOfFs); double getGPA() const; double printGPA() const; A destructor and an explicit default constructor initializing GPA=0.0 and checking if GPA>=0.0 by try{}catch{}. Add member function, bool operator<(const Student); The comparison in this function based on...
Hi, it's C++ question. Only can use <iostream> and <fstream>libraries Please help me, thanks Question Description: For this project you will write a program to: a) read-in the 10 first names from a file (the file is a priori given to have exactly 10 entries, of a maximum length of 8 letters each) into a 2-dimensional character array, b) output the names to the terminal with each one preceded by a number indicating its original order in the list, c)...
can someone please comment through this code to explain me
specifically how the variables and arrays are working? I am just
learning arrays
code is below assignment
C++
Programming from Problem Analysis to Program Design by D. S. Malik,
8th ed.
Programming
Exercise 12 on page 607
Lab9_data.txt
Jason, Samantha, Ravi,
Sheila, and Ankit are preparing for an upcoming marathon. Each day
of the week, they run a certain number of miles and write them into
a notebook. At the...
Hi this is C++, I'm really struggle with it please help me.... ************************ Here is the original high score program,: Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look exactly like this: Enter the name for score #1: Suzy Enter the score for...
Please program in C++ and document the code as you go so I can understand what you did for example ///This code does~ Your help is super appreciated. Ill make sure to like and review to however the best answer needs. Overview You will revisit the program that you wrote for Assignment 2 and add functionality that you developed in Assignment 3. Some additional functionality will be added to better the reporting of the students’ scores. There will be 11...
This lab is to give you more experience with C++ Searching and Sorting Arrays Given a file with data for names and marks you will read them into two arrays You will then display the data, do a linear search and report if found, sort the data, do a binary search. Be sure to test for found and not found in your main program. Read Data Write a function that reads in data from a file using the prototype below....
C++ problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have: A private member variable called dynamicArray that references a...
Write a program that works with two arrays of the same size that are related to each other in some way (or parallel arrays). Your two arrays must be of different data types. For example, one array can hold values that are used in a formula that produces the contents of the second array. Some examples might be: from a previous program, populations and the associated flowrates for those populations (an int array of populations and a double array...