Question

Pointers &Pointer arithmetic Memory allocation and freeing Main topics: Exercise This lab is designed to give you practice wo Need help about C PROGRAMMING,, pls use only C LANGUAGE.., yea so im doing this exercise as a practice for c programming, could someone do this as well so i could compare if my code makes makes sense and to see and help correct the errors im getting right now..
Any helpful help would be appreciated..
0 0
Add a comment Improve this question Transcribed image text
Answer #1

--------------------------------------------------------------------------------------------------------------------------

// Code Below

#include<stdio.h>
#include<stdlib.h>
typedef struct stack{
float *value;
int top;
int capacity;
} stack;

stack *pushvalue(stack *initStack,float val){
int i,j;
//if top == capacity then the stack is full
if(initStack->top == initStack->capacity){
//create a new stack twice the size
stack *newStack = (stack*)malloc(sizeof(stack)*1);
newStack->value = (float*)malloc(sizeof(float)*(2*initStack->capacity));
newStack->capacity = 2*initStack->capacity;
for(i=0;i<initStack->top;i++){
newStack->value[i] = initStack->value[i];
}
newStack->value[i] = val;
newStack->top = initStack->top+1;
free(initStack);
return(newStack);
}else{
initStack->value[initStack->top] = val;
initStack->top += 1;
return(initStack);
}
}


void printStackConfig(stack *s){
int i;
if(s->top == 0){
printf("The Stack is empty\n");
printf("\n");
}else{
printf("The top element of the stack is %.3f\n",s->value[s->top-1]);
printf("The total number of elements present in the stack is %d\n",s->top);
printf("The current capacity of the stack is %d\n",s->capacity);
printf("\n");
}
}
int main(){
int i,j,k,n;
stack initStack;
// initializing a new stack
initStack.capacity = 1;
initStack.top = 0;
initStack.value = (float*)malloc(sizeof(float)*initStack.capacity);

printStackConfig(&initStack);
stack *s1,*s2,*s3;
s1 = pushvalue(&initStack,0.5);
printStackConfig(s1);
s2 = pushvalue(s1,0.6);
printStackConfig(s2);
s3 = pushvalue(s2,0.75);
printStackConfig(s3);
s1 = pushvalue(s3,1.2);
printStackConfig(s1);
s2 = pushvalue(s1,2.34);
printStackConfig(s2);

return(0);
}

--------------------------------------------------------------------------------------------------------------------------

// Since the Chegg text editor removes '\n'(newline character) from the printf statements

// i am attaching screenshot of the function ' void printStackConfig(stack *s) ' which contains

// all the printf statements so that it becomes easy to know which printf statement contains '\n'

void printStackConfig(stack s) int i; if (s-top0) printf (The Stack is empty\n) printf (n): else printf (The top element

--------------------------------------------------------------------------------------------------------------------------

// ScreenShot of sample output obtained after running the code

The Stack is empty The top element of the stack is e.5ee The total number of elements present in the stack is 1 The current c

Add a comment
Know the answer?
Add Answer to:
Need help about C PROGRAMMING,, pls use only C LANGUAGE.., yea so im doing this exercise...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CS 241 Program 03 Due: Thursday, October 18th Main topics: Arrays& Pointers Memory allocation ram Specification:...

    CS 241 Program 03 Due: Thursday, October 18th Main topics: Arrays& Pointers Memory allocation ram Specification: A stack is a container that can be defined in terms of an array where all adds are preformed at the end of the sequence of existing values, and all removes are also preformed at end of the sequence of existing values. An empty stack is one that has no existing values in the array at all. We use the notion of top of...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • Note: The question needs to be answered in "C Programming Languange ". And after the question fin...

    Note: The question needs to be answered in "C Programming Languange ". And after the question find 3 pages for needed informations. Spring CE4717 Language Processors Q1. Consider the following LEx program. return R1 return R2 return R3 return R4 return R5; return R6; IA-2a-z)[A-Za-z0-9]- -2 10-91+ 10-9a-EA-FI Ihi] [01] [01] 이삐 t Vtin) int main (void) int tcode; do f tcode -yylex()i printf ("token type td \"%s\"\n", tcode, yytext); ) while (tcode)i return 0; i. Explain the steps needed...

  • In C Programming Language In this lab you will implement 4 string functions, two using array...

    In C Programming Language In this lab you will implement 4 string functions, two using array notation and two using pointers. The functions must have the signatures given below. You may not use any C library string functions. The functions are 1. int my strlen (char s ) - This function returns the number of characters in a string. You should use array notation for this function. 2. int my strcpy (char s [], char t I)- This function overwrites...

  • C++ programming help. The only change you have to make is in current program, where it...

    C++ programming help. The only change you have to make is in current program, where it says "your code goes here", do not make any changes to any other files, they are just there to show you. This code should be written for arbitrary data, not specifically for this sequence of data. The only place you need to write a code is marked YOUR CODE GOES HERE. You have been supplied a file with data in it (data2.txt). The line...

  • C programming. 1.Create a program that does the following - Creates three pointers, a character pointer...

    C programming. 1.Create a program that does the following - Creates three pointers, a character pointer professor, and two integer pointers student_ids, grades - Using dynamic memory, use calloc to allocate 256 characters for the professor pointer - Prompts the professor for their name, and the number of students to mark. - Stores the professor’s name using the professor pointer and in an integer the number of students to mark. - Using dynamic memory, use malloc to allocate memory for...

  • in C language we need to find the following : EXERCISE 3: 1. Write the definition...

    in C language we need to find the following : EXERCISE 3: 1. Write the definition of a structure named employee that contains character array members for an employee's first and last names, an int member for the employee's age, a char member that contains 'M' or 'F' for the employee's gender, and a double member for the employee's hourly salary. 2. Using the above mentioned definition, write a C program that asks a user to enter the values of...

  • I need help with my C coding homework. If possible can you please comment to help...

    I need help with my C coding homework. If possible can you please comment to help me understand whats going on in the code. Thanks in advance. Create a C program (3 points): o At the top of the file include the following comments: Your name The purpose (core concept found below) of the program The date created o Create two int variables: Initialize each to different values. o Create three pointers: Point the first two pointers to the first...

  • I need help with C, please don't copy other answers!! Thanks Declare a structure to represent...

    I need help with C, please don't copy other answers!! Thanks Declare a structure to represent a circle. Each circle stores information about the center and the radius. Center is represents with x and y coordinates. Both center and radius uses doubles. Allocate an array of 502 circle pointers and dynamically allocated memory to store a circle pointed by each array entry. Randomly generate circles using the following function. double rand_ float (double a, double b) {return ((double) rand()/RAND_MAX) *...

  • Purpose The purpose of the lab is to learn how to pass pointers in C. Process Create the function...

    Programming in C Purpose The purpose of the lab is to learn how to pass pointers in C. Process Create the functions as defined in the following table. Each function will add the last two integer parameters together Function PrototypeReturr int add2a(int,int); Returns the sum as a return value void add2b(int* int,int); Returns the sum in the first parameter int add2c(int*,int,int); Returns the sum as a return value and in the first parameter Returns the sum as a pointer to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT