Hi, I need help with this problem. I\'m supposed to create a vector calculator..
ID: 3621227 • Letter: H
Question
Hi, I need help with this problem.
I'm supposed to create a vector calculator...well not really.
We're supposed to create functions and test them with TDD (Test Driven Development) and only implement the functions I have in this code.
I'm having problems allocating a new data structure(Vector).
Can anyone help?
#include <assert.h> // assert macro
#include <cstdlib>
#include <stddef.h> // size_t, NULL
#include <stdio.h> // FILE, *printf, *scanf, fopen,...
#include <stdlib.h> // calloc, free, rand, *sort, ...
#include <string.h> // str???, bcopy, bzero, ...
using namespace std;
//==================================================================================
// Elem is a value that the user enters for the vector calculator
typedef int Elem;
//==================================================================================
// Contains an unsigned integer field for recording size of vector, plus a pointer to dynamic allocated array
typedef struct {
unsigned int Vec_size; // Size of the Vector
float *Vec; // Pointer to a dynamically allocated array
}Vector;
//==================================================================================
// print_vec:
// print vectors to a file descriptor.
// In:
//
// Out:
// Vectors printed to screen
void print_vec(Vector *newVector) {
for (unsigned int i = 0; x < newVector->Vec_size; i++){
printf(" %f", newVector->Vec[i]);
}
}
//==================================================================================
// alloc_vec
// Allocate an empty vector
// In:
//
// Out:
// return -- Vector with empty elements
Vector *alloc_vec(void) {
Vector *newVector = new Vector;
if (NULL == newVector)
return NULL;
}
//==================================================================================
// dealloc_vec
// frees the memory
// In:
//
// Out:
// Memory is deallocated
// No return value.
void dealloc_vec(Vector *) {
}
//==================================================================================
// Instruct user on how to use this program; i.e. the commands it
// supports and the syntax of those commands.
// In:
// none
// Out:
// output produced on standard output
void help_vec(void) {
puts( " Usage:" );
puts( " p - print vector" );
puts( " q,e - quit, end" );
puts( " h - print usage help" );
puts( " + <operand> - add <operand> to each element of vector" );
puts( " - <operand> - subtract <operand> from each element of vector" );
puts( " * <operand> - multiple each element of vector by <operand>" );
puts( " / <operand> - divide each element of vector by <operand>" );
puts( " a <value> - extend vector by additional value" );
}
//==================================================================================
// main:
// Program entry point
// In:
//
//
// Out:
// return -- EXIT_SUCCESS if program terminates normally,
// EXIT_FAILURE otherwise
int main(int, char**) {
return EXIT_SUCCESS;
}
Explanation / Answer
i thing the alloc_vec function is wrong this is the correct bcs u create newVector but u don't return the poiter of newVector. Vector *alloc_vec(void) { Vector *newVector = new Vector; if (NULL == newVector) return NULL; else return newVector; }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.