Test.c ~~~~~~~~~~~~ #include <stdio.h> #include <stdlib.h> #include \"vector.h\"
ID: 3794272 • Letter: T
Question
Test.c
~~~~~~~~~~~~
#include <stdio.h>
#include <stdlib.h>
#include "vector.h"
int main(int argc, char **argv) {
vector_t *v;
printf("Calling vector_new() ");
v = vector_new();
printf("Calling vector_delete() ");
vector_delete(v);
printf("vector_new() again ");
v = vector_new();
printf("These should all return 0 (vector_get()): ");
printf("%d ", vector_get(v, 0));
printf("%d ", vector_get(v, 1));
printf("%d ", vector_get(v, 2));
printf("%d ", vector_get(v, 3));
printf("%d ", vector_get(v, 4));
printf("%d ", vector_get(v, 5));
printf("%d ", vector_get(v, 6));
printf("%d ", vector_get(v, 7));
printf("%d ", vector_get(v, 8));
printf("%d ", vector_get(v, 9));
printf("%d ", vector_get(v, 10));
printf("%d ", vector_get(v, 100));
printf("%d ", vector_get(v, 1000));
printf("%d ", vector_get(v, 10000));
printf("%d ", vector_get(v, 100000));
printf("%d ", vector_get(v, 1000000));
printf("%d ", vector_get(v, 10000000));
printf("Doing a bunch of vector_set()s ");
vector_set(v, 0, 98);
vector_set(v, 11, 15);
vector_set(v, 15, -23);
vector_set(v, 24, 65);
vector_set(v, 12, -123);
vector_set(v, 15, 21);
vector_set(v, 25, 43);
printf("These should be equal: ");
printf("98 = %d ", vector_get(v, 0));
printf("15 = %d ", vector_get(v, 11));
printf("65 = %d ", vector_get(v, 24));
printf("-123 = %d ", vector_get(v, 12));
printf("21 = %d ", vector_get(v, 15));
printf("43 = %d ", vector_get(v, 25));
printf("Test complete. ");
return 0;
}
Vector.c
~~~~~~~~~~~~~
/* Include the system headers we need */
#include <stdlib.h>
#include <stdio.h>
/* Include our header */
#include "vector.h"
/* Define what our struct is */
struct _vector_t {
size_t size;
int *data;
};
/* Create a new vector */
vector_t *vector_new() {
vector_t *retval;
/* First, we need to allocate the memory for the struct */
retval = (vector_t *)malloc(1 * sizeof(vector_t));
/* Check our return value to make sure we got memory */
if(retval == NULL)
return NULL;
/* Why does the above statement cast the malloc's return value to 'vector_t *'
* instead of 'struct _vector_t *'? Does it matter?
*/
/* Now we need to initialize our data */
retval->size = 1;
retval->data = (int *)malloc(retval->size * sizeof(int));
/* Check our return value to make sure we got memory */
if(retval->data == NULL) {
free(retval);
return NULL;
}
retval->data[0] = 0;
/* Note that 'retval->size' could be written '(*retval).size', but the ->
* convention is easier to read
*/
/* and return... */
return retval;
}
/* Free up the memory allocated for the passed vector */
void vector_delete(vector_t *v) {
/* Remember, you need to free up ALL the memory that is allocated */
/* ADD CODE HERE */
}
/* Return the value in the vector */
int vector_get(vector_t *v, size_t loc) {
/* If we are passed a NULL pointer for our vector, complain about it and
* return 0.
*/
if(v == NULL) {
fprintf(stderr, "vector_get: passed a NULL vector. Returning 0. ");
return 0;
}
/* If the requested location is higher than we have allocated, return 0.
* Otherwise, return what is in the passed location.
*/
if(loc < v->size) {
return v->data[loc];
} else {
return 0;
}
}
/* Set a value in the vector */
void vector_set(vector_t *v, size_t loc, int value) {
/* What do you need to do if the location is greater than the size we have
* allocated? Remember that unset locations should contain a value of 0.
*/
/* ADD CODE HERE */
}
Vector.h
~~~~~~~~~~~~~
#ifndef _CSE31_VECTOR_H_
#define _CSE31_VECTOR_H_
/* vector.h written by Jeremy Huddleston <jeremyhu@eecs.berkeley.edu> Sp2004
*
* So it looks like you've decided to venture into the "other" files of this
* lab. Good. C Header files (the .h extension) are a way of telling other .c
* files what they can have access to. You usually include stdlib.h in your
* C programs, and this process is identical to including this .h file with the
* one change being:
*
* #include "file.h"
* versus
* #include <file.h>
*
* The difference is that the <> notation is for system header files and the ""
* is for ones you provide yourself (in your local directory for instance).
*
* The header file starts off with
* #ifndef _CSE31_VECTOR_H_
* #define _CSE31_VECTOR_H_
*
* and ends with a final #endif. This prevents the file from being included
* more than once which could've possibly resulted in an infinite loop of
* file inclusions.
*
* First, we define the 'vector_t' datatype. This next line says that a 'vector_t'
* is the same as a 'struct _vector_t'. So anywhere in the code after this, we
* can use 'vector_t *' to mean a pointer to a 'struct _vector_t' (which is defined in
* vector.c). We can get away with doing this even though we don't know what a
* struct vector is because all pointers take up the same ammount of memory
* space.
*/
#include <sys/types.h>
typedef struct _vector_t vector_t;
/*
* Next, we provide the prototypes for the functions defined in vector.c. This
* is a way of telling the .c files that #include this header what they will
* have access to.
*/
/* Create a new vector */
vector_t *vector_new();
/* Free up the memory allocated for the passed vector */
void vector_delete(vector_t *v);
/* Return the value in the vector */
int vector_get(vector_t *v, size_t loc);
/* Set a value in the vector */
void vector_set(vector_t *v, size_t loc, int value);
#endif
Explanation / Answer
# include <iostream.h>
# include <conio.h>
# include <stdlib.h>
class searching
{
int a[100],n;
public:
void accept()
{
cout<<"ENTER N VALUE ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"ENTER "<<i<<" Value ";
cin>>a[i];
}
}
void print()
{
for(int i=0;i<n;i++)
cout<<a[i]<<endl;
}
void search()
{
int num;
cout<<"ENTER A NUMBER TO FIND ";
cin>>num;
for(int i=0;i<n;i++)
if(a[i]==num)
{
cout<<"The number found at "<<i<<" Location";
getch();
exit(0);
}
cout<<"The number not foud ";
}
};
void main()
{
clrscr();
searching s;
s.accept();
s.print();
s.search();
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.