Developing a program in procedural C C not including Classes

Developing a program in \"procedural C++\" (C++ not including Classes/objects, templates, etc.) called vecalc which is a simple, interactive vector calculator. Using good programming style, and on following the test-driven development (TDD) process. A \"vector\" in the context of this assignment has meaning taken from linear algebra: it is simply a one-dimension array.

The vecalc program operates on a retained vector of (single-precision) floating-point values. It alternates between two states, an input state and a calculation (or calculator) state. The program begins execution in the former state, wherein it creates an empty vector, prompts the user for a number of elements in the vector, and builds up a vector with elements obtained one-at-a-time from the user. The program then switches to calculation state. In calculation state vecalc accepts operations and commands from the user and executes them. After most commands the program remains in calculation state. On a \"clear\" command (\"c\"), the program frees up the memory used by the current vector (i.e. deletes that exiting vector) and switches back to input state.

Elements of a vector are to be of type float. A vector can contain from 0 up to 65535 elements.

Input is taken from stdin and normal output goes to stdout. Error output goes to stderr.

The operations and commands supported by vecalc in calculator state are as follows:

vecalc uses dynamically-allocated memory for the vector of floating-point values. The vector is allocated using new and de-allocated using delete on the \"c\", \"e\", or \"q\" commands. An \"a\" requires a combination of allocating a new vector, copying the contents of the old vector, adding the additional element, and then de-allocating the old vector.

In terms of interaction with a user, the vecalc program is reasonably robust. For instance:

it terminates gracefully if the user gives an end-of-file when the program is seeking input;

it checks if the value given in a \"/\" command is zero, and takes reasonable action if it is;

it tolerates reasonable amounts of white space (including no white space) between the operator and operand in the case of \"+\", \"-\", \"*\", and \"/\";

it takes a reasonable course of action if the user provides only white space or a null line when input is sought.

For obtaining numeric values from the user, the program may

get input using fgets(3), and convert it using atoi(3), atof(3), or sscanf(3), or

use scanf(3) or fscanf(3)

Data Types: Elem – for a vector element. Vector – a structured type for vectors. This structure contains an unsigned integer field for recording the size of the vector, plus a pointer to a dynamically allocated array containing the vector elements.

Supporting Functions: bool print_vec(Vector *); Vector *alloc_vec(void) — allocate an empty (zero-length) vector; void dealloc_vec(Vector *); Vector *extend_vec(Vector *, Elem) — allocate a new vector one element greater in size than the input vector, copy the elements in the input vector to the new one, add the new element to the end of the new vector, and return a pointer to the new vector. The input vector is not modified; Vector *scalar_plus(Vector *, Elem)

In addition you need to implement stub routines for the following functions. These stubs are to be commented, but must return an arbitrary value (NULL in this case) so that the corresponding unit tests will fail.

Vector *scalar_minus(Vector *, Elem)

Vector *scalar_mult(Vector *, Elem)

Vector *scalar_div(Vector *, Elem)

q : quit; same functionality as \"e\" (end) below
c : clear; free up the dynamically-allocated memory in use for the current vector and revert back to the input state
p : print; print the current value of the vector
h : help; print a summary of the commands and operations possible
a value : append; extend the vector with the additional floating-point value specified
+ value : scalar plus; add the specified floating-point value to each element in the vector
- value : scalar minus; subtract the specified floating-point value from each element in the vector
* value : scalar multiply; multiply each element in the vector by the specified floating-point value
/ value : scalar divide; divide each element in the vector by the specified floating-point value
e : end; free up the dynamically-allocated memory in use for the current vector and terminate execution

Solution

Given below are the files needed for the question. Output shown at end. Please don\'t forget to rate the answer if it helped. Thank you.

Vector.h


#ifndef VECTOR_H
#define VECTOR_H
#include <iostream>
using namespace std;
typedef float Elem;
struct Vector
{
int size;
Elem *data;
};

bool print_vec(Vector *v)
{
if(v == nullptr) return false;
for(int i = 0; i < v->size; i++)
cout << v->data[i] << \" \" ;
cout << endl;
return true;
}
Vector *alloc_vec(void) // allocate an empty (zero-length) vector;
{
Vector *v = new Vector;
v->size = 0;
v->data = nullptr;
return v;
}
void dealloc_vec(Vector *v)
{
if(v != nullptr)
{
if(v->data != nullptr)
delete [] v->data;
delete v;
}
  
}
/* allocate a new vector one element greater in size than the input vector, copy the elements in the input vector to the new one, add the new element to the end of the new vector, and return a pointer to the new vector. The input vector is not modified; */

Vector *extend_vec(Vector *v, Elem e)
{
  
Vector *v1 = new Vector;
if(v != nullptr)
v1->size = v->size + 1;
else
v1->size = 1;
v1->data = new Elem[v1->size];
  
if(v != nullptr)
{
for(int i = 0; i < v->size; i++) //copy all existing
v1->data[i] = v->data[i];
}
  
v1->data[v1->size - 1] = e; // copy to end
return v1;
  
}
Vector *scalar_plus(Vector *v, Elem e)
{
if(v == nullptr) return nullptr;
  
Vector *v1 = new Vector;
v1->size = v->size;
if(v1->size == 0)
v1->data = nullptr;
else
{
v1->data = new Elem[v1->size];
  
for(int i = 0; i < v->size; i++) //add to each element
v1->data[i] = v->data[i] + e;
}
return v1;
}

Vector *scalar_minus(Vector *v, Elem e)
{
if(v == nullptr) return nullptr;
Vector *v1 = new Vector;
v1->size = v->size;
if(v1->size == 0)
v1->data = nullptr;
else
{
v1->data = new Elem[v1->size];
  
for(int i = 0; i < v->size; i++) //add to each element
v1->data[i] = v->data[i] - e;
}
return v1;
}
Vector *scalar_mult(Vector *v, Elem e)
{
if(v == nullptr) return nullptr;
  
Vector *v1 = new Vector;
v1->size = v->size;
if(v1->size == 0)
v1->data = nullptr;
else
{
v1->data = new Elem[v1->size];
  
for(int i = 0; i < v->size; i++) //add to each element
v1->data[i] = v->data[i] * e;
}
return v1;
}
Vector *scalar_div(Vector *v, Elem e)
{
if(v == nullptr || e == 0) return nullptr;
  
Vector *v1 = new Vector;
v1->size = v->size;
if(v1->size == 0)
v1->data = nullptr;
else
{
v1->data = new Elem[v1->size];
  
for(int i = 0; i < v->size; i++) //add to each element
v1->data[i] = v->data[i] / e;
}
return v1;
}

#endif /* VECTOR_H */

vecalc.cpp


#include \"Vector.h\"
#include <iostream>
#include <cctype>
void displayHelp();
int main()
{
Vector *v = alloc_vec(), *old, *result;
int n;
Elem value;
bool inputMode = true;
string command = \" \";
bool stop = false;;
  
while(!stop)
{
  
cout << \"How many elements ? \";
cin >> n;
old = v;
for(int i = 1; i <= n; i++)
{
cout << \"Enter element \" << i << \": \" ;
cin >> value;
v = extend_vec(old, value);
dealloc_vec(old); //dealocate the old object
old = v;
}
  
inputMode = false;
cout << \"Enter the command at prompt (type h for help)\" << endl;
string command;
  
while(!stop && !inputMode)
{
cout << \"> \";
cin >> command;
if(command.length() != 1)
cout << \"Error: Unknown command - \" << command << endl;
else
{
  
switch(tolower(command[0]))
{
case \'c\':
dealloc_vec(v);
v = alloc_vec();
inputMode = true;
break;
case \'p\':
print_vec(v);
break;
case \'h\':
displayHelp();
break;
case \'q\':
case \'e\':
dealloc_vec(v);
stop = true;
break;
case \'a\':
cin >> value;
old = v;
v = extend_vec(old, value);
dealloc_vec(old);
cout << value << \" appended. vector is \" ;
print_vec(v);
break;
case \'+\':
cin >> value;
result = scalar_plus(v, value);
cout << \"result = \";
print_vec(result);
dealloc_vec(result);
break;
  
case \'-\':
cin >> value;
result = scalar_minus(v, value);
cout << \"result = \";
print_vec(result);
dealloc_vec(result);
break;
  
case \'*\':
cin >> value;
result = scalar_mult(v, value);
cout << \"result = \";
print_vec(result);
dealloc_vec(result);
break;
  
case \'/\':
cin >> value;
if(value == 0)
cout << \"Error: Division by zero \" << endl;
else
{
result = scalar_div(v, value);
cout << \"result = \";
print_vec(result);
dealloc_vec(result);
}
break;
default:
cout << \"Error: Unknown command - \" << command << endl;
  
}
}
}
}
  
  
}

void displayHelp()
{
cout << \"q - quit\" << endl;
cout << \"c - clear current vector and go to input state\" << endl;
cout << \"p - print current vector\" << endl;
cout << \"h - print help summary\" << endl;
cout << \"a <value> - append the value to vector\" << endl;
cout << \"+ <value> - scalar addition, add <value> to each element in vector\" << endl;
cout << \"- <value> - scalar subtracton, subtract <value> from each element in vector\" << endl;
cout << \"* <value> - scalar multiplication, multiply <value> to each element in vector\" << endl;
cout << \"/ <value> - scalar divide, divide each element in vector by <value>\" << endl;
cout << \"e - end , quit\" << endl;
}

output

How many elements ? 5
Enter element 1: 2
Enter element 2: 4
Enter element 3: 6
Enter element 4: 8
Enter element 5: 10
Enter the command at prompt (type h for help)
> h
q - quit
c - clear current vector and go to input state
p - print current vector
h - print help summary
a <value> - append the value to vector
+ <value> - scalar addition, add <value> to each element in vector
- <value> - scalar subtracton, subtract <value> from each element in vector
* <value> - scalar multiplication, multiply <value> to each element in vector
/ <value> - scalar divide, divide each element in vector by <value>
e - end , quit
> a 12
12 appended. vector is 2 4 6 8 10 12
> p
2 4 6 8 10 12
> a 14
14 appended. vector is 2 4 6 8 10 12 14
> + 5
result = 7 9 11 13 15 17 19
> - 4
result = -2 0 2 4 6 8 10
> * 3
result = 6 12 18 24 30 36 42
> / 0
Error: Division by zero
> / 2
result = 1 2 3 4 5 6 7
> c
How many elements ? 3
Enter element 1: 5
Enter element 2: 10
Enter element 3: 15
Enter the command at prompt (type h for help)
> / 2
result = 2.5 5 7.5
> q

Developing a program in \
Developing a program in \
Developing a program in \
Developing a program in \
Developing a program in \
Developing a program in \
Developing a program in \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site