CS 1050 Prelab10 Directions Complete the following prelab as

CS 1050 Pre-lab10 Directions:

Complete the following pre-lab assignment before coming to next week’s lab. It will help you in completing the lab assignment. If you have any questions or have problem in completing the pre-lab ask TAs for help during TA office hours or schedule an appointment.

Purpose: 1. Use and extend the concepts learned from the previous labs. 2. Use pointer notation and pointer arithmetic in implementation. 3. Learn and use the dereferencing operator.

Submission information: Submit this assignment by following the instructions given by your TA. SUBMIT ONLY the .c file (no a.out or executable file is required). All the lab assignments must be submitted before the end of the lab using the lab code given by the TA. Use the following submit command. Filename must be: sectionletter-prelab10.c (Include your respective lab section) e.g. f-prelab10.c $ submit $ submit CS1050 PRELAB10 f-prelab10.c

Description: Implement following functions for the prelab assignment.

void initialize_array(int *, int ): This function takes an integer pointer and the input size and stores random numbers using the integer pointer. The random numbers range from the value 1 to 5 only.

void print_array(int *, int): This function takes an integer pointer and the input size and prints out random numbers using the integer pointer.

int check_size(int): This function takes an integer number and checks if the number is between 1-100 or not. If it is, it returns 1, otherwise returns 0.

main(): First read the input size from the user, performing an error check using the check_size function. Call functions initialize_array and print_array to store and print the random numbers. Display the results as shown in the sample output below.

Note: 1. In the main declare an integer pointer and malloc space for it based on the size the user enters, assign the address of this pointer/array to another integer pointer and pass this pointer to all the functions. 2. Use only pointer notation and pointer arithmetic to implement the assignment. 3. Use the relevant library function, the ones you have used already. 4. For the LAB NEXT WEEK, come prepared with how to use % operator and some basic stats like average, sorting an array and basically traversing though an array of integers.

Sample output

Characters in bold are input from the user.

[sm3z5@babbage lab10]$ ./a.out Enter the size of the input:

-12 Invalid input enter the size of the array again:

123 Invalid input enter the size of the array again: 0 Invalid input enter the size of the array again:

6 Input array

1 2 1 5 2 5

[sm3z5@babbage lab10]$ ./a.out Enter the size of the input:

-1 Invalid input enter the size of the array again: 0 Invalid input enter the size of the array again:

0 Invalid input enter the size of the array again:

0 Invalid input enter the size of the array again:

101 Invalid input enter the size of the array again:

8 Input array 5 4 4 3 5 1 2 3

[sm3z5@babbage lab10]$ ./a.out

Enter the size of the input: 3 Input array 1 4 1

Solution

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//prototypes
void initialize_array(int *, int);
void print_array(int *, int);
int check_size(int);

#define MAX 100
int main(void)
{   int a[MAX] = { 0 };
   int *ptr=0; //integer pointer
   ptr=a; //point p at a
   //ask user for array size
   int input_size=0;
   printf(\"Enter the size of the input: \");
   do{
       scanf(\"%d\", &input_size);
       if(!check_size(input_size))
       {
           printf(\"Invalid input enter the size of the array again: \");
       }
   }while(!check_size(input_size)); //error check using check_size()
   //initialize array with given size
   initialize_array(ptr, input_size);
   //print array
   printf(\"\ Input Array\ \");
   print_array(ptr, input_size);
   return 0;
}

void initialize_array(int *p, int size)
{ //takes int pointer and int size and stores random numbers using the pointer.
   srand(time(NULL)); //seed rng
   int i=0;
   for(i=0; i<size; i++)
   {
       *(p+i)=rand()%10; //store random number at location pointed to by *p
   }
   //other option *p = rand()%10; p++;
}

void print_array(int *p, int size)
{ //takes int pointer and input size and prints out random numbers using the pointer
   int i=0;
   for(i=0; i<size; i++)
   {
       printf(\"%d \", *(p+i)); //print each element in array using ptr notation
   }
   printf(\"\ \");
}

int check_size(int size)
{ //takes int number and checks if the number is between 0-100
   if(size>0&&size<=100)
   {
       return 1;
   }
   else
   {
       return 0;
   }
}

CS 1050 Pre-lab10 Directions: Complete the following pre-lab assignment before coming to next week’s lab. It will help you in completing the lab assignment. If
CS 1050 Pre-lab10 Directions: Complete the following pre-lab assignment before coming to next week’s lab. It will help you in completing the lab assignment. If
CS 1050 Pre-lab10 Directions: Complete the following pre-lab assignment before coming to next week’s lab. It will help you in completing the lab assignment. If

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site