EECS168:Lab6

From ITTC
Jump to: navigation, search
Navigation
Home
Information

Syllabus
Schedule
Exam Reviews

Classwork

Labs
Submitting Work

Due Time

  • The lab is due Friday July 17th by 11:59pm

Lab conduct

  • Do not use any unauthorized aid, such sites like rentacoder or chegg to obtain answers
  • Do not use code provided by another student
  • Do not reuse code (by you or anyone) from prior semesters
  • If you need help, seek it from:
    • Your lab TA.
    • Me, Dr. Gibbons, my email is jwgibbo@ku.edu / jwgibbo@gmail.com
  • If equipment you don't own (e.g. cycle servers) needs attention or you're having account issues put in a ticket!

Topics

  • Command line arguments
  • Converting strings
  • Resizing arrays

Command line arguments

You can pass information into your program at the command - in other words, from terminal.

Example:

$>./myLab coffee eggs bread

In addition to launching your program, you passed 3 pieces of information, "coffee", "eggs", and "bread" into the program. Where is it? In a 2D character array!

You main() will be update to use command line arguments.

//old main
int main ()
{
   //stuff
}
//new main
int main( int argc, char** argv )
{
   //stuff
}

Question: What are argc and argv?

  • argc
    • a count of how many command line arguments (include the program's name) were passed in.
    • Our example from above would set argc to 4
  • argv
    • A 2D character array with all the words passed in


Question: How do we use them? Let's say I want to store one of the argument in a string. It's easy!

//new main
int main( int argc, char** argv )
{
   std::string myStr;
   //check to see if there's an argument to grab
   if( argc > 1 )
   {
      myStr = argv[1]; //copies the argument into your variable
   }
}

Converting Strings

There are functions that you can use, after you include the <string> library in your program, that can convert std::string to int or double.

Example

std::string str1 = "42";
std::string str2 = "2.5";

int x = 0;
double d = 0;

x = std::stoi(str1);  //string to int 
d = std::stod(str2);  //string to double

Myths about arrays

  • You can change the size of an array
    • Wrong! Once an array is created it's size cannot change, but you can create new array of a different size and copy any desired value over
  • Arrays are passed by value to functions
    • Wrong! When you pass an array to a function , what you are actually passing is the array reference (aka a pointer to the first block of memory in the array). And since an array doesn't know its own size, the size will need to be passed as well. Let this example illustrate:

Exercise: Array Resizing

You will create an array manipulation program that allows the user to do pretty much whatever they want to an array.

  1. When launching the program, the user will pass in a file name that contains a set of value and an int that informs the program how many values are in the file (see example below)
  2. Check to see if the file could be opened. Exit the program if it was not opened, otherwise continue
  3. Create an array and fill it with the values from file
  4. Present the user with a menu, detect their choice, and provide them any needed follow up prompts that are needed.
  5. Continue until they want to quit

Example command line:

$>./lab09 nums.txt 5

This indicates there is a file named "nums.txt" with 5 values in it.

Example nums.txt:

2
77
31
3
6


Menu Option Description
Insert
  • The user will provide a position to insert a value
  • You must obtain a valid position before moving on
  • Obtain the value to insert and insert it into the array
  • NOTE: The array will be one element larger after
Remove
  • The user will provide a position to remove a value
  • You must obtain a valid position before moving on
  • Once you have a valid position, remove that value
  • NOTE: The array will be one element smaller after
Count
  • Obtain a value from the user
  • Tell them how many times that value is in the array
Print
  • Print the contents of the array in the following format:
[1, 3, 99]
Exit
  • Exits the program

Sample Menu

Remember, this menus comes after the user fills the initial array.

Make a selection:
1) Insert
2) Remove
3) Count
4) Print
5) Exit
Choice: 

Additional Requirements

For each of the options the user has access to, create a function to handle the work involved.

  • int* insert(int arr[ ], int& size, int value, int position)
    • Inserts the given value at the specified position
    • Creates a new array, copies all old value over adjusting indices as necessary
    • Deletes the old array (arr)
    • Updates the size (that's why it's passed by reference)
    • Returns a pointer to the new array
  • int* remove(int arr[ ], int& size, int position)
    • Removes the value at the given position
    • Creates a new array, copies all old value over adjusting indices as necessary
    • Deletes the old array (arr)
    • Updates the size (that's why it's passed by reference)
    • Returns a pointer to the new array
  • int count(int arr[ ], int size, int target)
    • returns a count of how many times the target value is in the array
  • void print(int arr[ ], int size)
    • Prints array as required


Rubric

  • [10pts] Handles command line arguments
  • [5pts] Verifies file is open
  • [25pts] Insert
  • [25pts] Remove
  • [10pts] Count
  • [5pts] Print
  • [5pts] Exit
  • [5pts] No segmentation faults (all or nothing)
  • [10pts] No memory leaks (all or nothing)