Question

***************C PROGRAMMING ONLY************* Demonstrate the ability to create an array on the stack Demonstrate the ability...

***************C PROGRAMMING ONLY*************

  • Demonstrate the ability to create an array on the stack
  • Demonstrate the ability to create an array on the heap allowing user to choose the number of values to store.
  • Demonstrate the ability to store an array of Struct values on both the stack and the heap.

Program Specifications:

1. Create a struct with at least 3 fields - any struct you want but explain it in your comments in the code. Populate at least 10 elements in an array of elements of this struct type with a limit of 1000 elements.

2. Create an array on the heap to store the same number of populated elements from the stack array.

3. Copy the values from the stack array into the dynamic array.

The program will output the contents from each array, one at a time.

** You can have a menu system if you wish but it is not a requirement **

4. Finally, create a BIN file to store the contents of the array on the heap.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// C program to create array of structures on Stack and Heap

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

// struct representing a student

typedef struct

{

               int id; // id of student

               int score; // score of student

               char grade; // grade of student

}student;

int main(void) {

               srand(time(NULL));

               const int MAXSIZE = 1000;

               student students_stack[MAXSIZE];

               int numElements = 10;

               int i;

               // loop to populate 10 elements in array in stack

               for(i=0;i<numElements;i++)

               {

                              students_stack[i].id = i+1;

                              students_stack[i].score = rand()%101;

                              if(students_stack[i].score >=90)

                                             students_stack[i].grade = 'A';

                              else if(students_stack[i].score >=80)

                                             students_stack[i].grade = 'B';

                              else if(students_stack[i].score >=70)

                                             students_stack[i].grade = 'C';

                              else if(students_stack[i].score >=50)

                                             students_stack[i].grade = 'D';

                              else

                                             students_stack[i].grade = 'F';

               }

               // Create an array on the heap to store the same number of populated elements from the stack array.

               student *students_heap = (student*)malloc(numElements*sizeof(student));

               // if memory not allocated, exit

               if(students_heap == NULL)

               {

                              printf("\n Memory not allocated");

                              return EXIT_FAILURE;

               }

               //Copy the values from the stack array into the dynamic array.

               for(i=0;i<numElements;i++)

               {

                              students_heap[i].id = students_stack[i].id;

                              students_heap[i].score = students_stack[i].score;

                              students_heap[i].grade = students_stack[i].grade;

               }

               // print the elements from both the array

               for(i=0;i<numElements;i++)

               {

                              printf("\n Stack : ");

                              printf(" ID : %d Score : %d Grade : %c",students_stack[i].id,students_stack[i].score,students_stack[i].grade);

                              printf("\n Heap : ");

                              printf(" ID : %d Score : %d Grade : %c",students_heap[i].id,students_heap[i].score,students_heap[i].grade);

               }

               // write contents of heap to binary file

               FILE *filePtr = fopen("students.bin","w");

               if(filePtr == NULL)

               {

                              printf("Unable to open file");

                              return EXIT_FAILURE;

               }

               for(i=0;i<numElements;i++)

               {

                              if(i != numElements-1)

                                             fprintf(filePtr,"%d %d %c\n",students_heap[i].id,students_heap[i].score,students_heap[i].grade);

                              else

                                             fprintf(filePtr,"%d %d %c",students_heap[i].id,students_heap[i].score,students_heap[i].grade);

               }

               fclose(filePtr);

               return EXIT_SUCCESS;

}

//end of program

Output:

Add a comment
Know the answer?
Add Answer to:
***************C PROGRAMMING ONLY************* Demonstrate the ability to create an array on the stack Demonstrate the ability...
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
  • Java Programing Code only Ragged Array Assignment Outcome: Student will demonstrate the ability to create and...

    Java Programing Code only Ragged Array Assignment Outcome: Student will demonstrate the ability to create and use a 2D array Student will demonstrate the ability to create and use a jagged array Student will demonstrate the ability to design a menu system Student will demonstrate the ability to think Program Specifications: Write a program that does the following: Uses a menu system Creates an array with less than 25 rows and greater than 5 rows and an unknown number of...

  • C++ Create an array-based implementation of a stack. Each element of the stack should store a...

    C++ Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a...

  • *MUST BE IN C PROGRAMMING* Demonstrate the ability to think critically Demonstrate the ability to work...

    *MUST BE IN C PROGRAMMING* Demonstrate the ability to think critically Demonstrate the ability to work with strings and chars Demonstrate the ability to design a menu system Write a simple program that allows the user to enter a number between 1 and 1000.  The program will then display the Roman numeral equivalent.  Allow the user to keep entering numbers for conversion to Roman numerals, or to quit, using a menu system of your own design. Submission Requirements: You are to write...

  • Language: C Programming, Please leave as many comments as possible! Create a program that has a...

    Language: C Programming, Please leave as many comments as possible! Create a program that has a menu as shown: 1. Generate 500 Random Numbers between 1 and 500 2. Show Current Set of Random Numbers ordered from Low To High 3. Show Frequency of each number from 1 to 500 4. Quit The program will store all random number in an array on the heap.

  • Methods and File Output and Exceptions Outcome: Student will demonstrate the ability to write, use and...

    Methods and File Output and Exceptions Outcome: Student will demonstrate the ability to write, use and call methods Student will demonstrate the ability to pass values to and from methods Student will demonstrate the ability to catch exceptions Student will demonstrate the ability to create a text file Student will demonstrate the ability to validate input data Program Specifications: You to write a menu driven program. The program will use a switch statement. The cases will be as follows: Get...

  • I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and...

    I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and size using below pseudo code. I need two classes with stack pseudo code implementation and the main method to demonstrate the correct working of each operation. pseudo code StackADT (using raw array) class StackADT { int top int items[] int max StackADT(int n) Initialize array to n capacity top = 0 max = n boolean isEmpty() if array has no elements return true else...

  • Following the instruction This is c++ programming Lab Tasks: 1. Define a dynamic array class in...

    Following the instruction This is c++ programming Lab Tasks: 1. Define a dynamic array class in DynamicArray .h and DynamicArray.cpp files, according to the following UML class diagram: DynamicArray - int arrySize; - int currentSize; int* arrayPtr; + DynamicArray(int size) // Explicit constructor, which you define- allocate space in dynamic memory for an integer array of the given size. + DynamicArray) // Explicit destructor, which you define-de allocate dynamic memory. + additem(int item): bool // Set the value of the...

  • Create a program that uses a Jagged Array. The program will create an array with 50...

    Create a program that uses a Jagged Array. The program will create an array with 50 rows. Each of the values for each element of the array will be randomly generated. The random values will be between 1 and 20. Depending on the value generated for each row, you will create an array with that number of columns for that row. Each column created will contain the value to the left plus 1. After you create and populate the entire...

  • Can someone help me with these C program problems? Thanks 1. Write a C program that...

    Can someone help me with these C program problems? Thanks 1. Write a C program that creates two arrays of integers, one on the stack and one on the heap. You will loop through these arrays and populate them with values (any value is fine). You will then loop through these arrays again and print each value. Your output should look something like “Value in index 1 is 100 from stack array and 100 from heap array.” Do not forget...

  • (C++) Write a function that accepts an int array and the array’s size as arguments. The function...

    (C++)Write a function that accepts an int array and the array’s size as arguments.The function should create a new array that is twice the size of the argument array.The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0.The function should return a pointer to the new array.Demonstrate the function by using it in a main program that reads an integer N (that is not more...

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