ELEC 1520, Pointers Homework 40 points Submission Instructions: Your homework wi
ID: 3863440 • Letter: E
Question
ELEC 1520, Pointers Homework 40 points Submission Instructions: Your homework will be submitted electronically to Canvas. The file format must be a zip file. Your file should be named elec 1520 hwpointers your name,zip. Substitute your first and last name for "your name" in the file name. Example: Students name is Sparky Watts. The file name is elec 1520 hwpointers sparky. watts zip. write separate c program for problems 1-4. Name the hwpointers 02.c, hwpointers 03.c, and hwpointers o4.c. Place these in your compressed file. The program must include the following information in comments: (1) problem number, (2) course information, (3) assignment, (4) date, and (5) student name. For problems that do not specifically ask for separate function, you may write all of your code in main. If you wish to write other functions, you are encouraged to do so. You will submit one .c file for each separate problem. Any program that uses random numbers must call srand(time(NULLI) in main to seed the random number generator. Note that srand is only called once in your program. Dell Update 2 updates areExplanation / Answer
1.
#include<stdio.h>
int main(){
int *pintgr; /* pointer to an integer */
double *pdouble; /* pointer to a double */
float *pfloat; /* pointer to a float */
char *pchar; /* pointer to a character */
short *pshort;
printf("Size of variables pchar is %d bytes",sizeof(pchar));
printf(" pshort is %d bytes",sizeof(pshort));
printf(" pintgr is %d bytes",sizeof(pintgr));
printf(" pdouble is %d bytes",sizeof(pdouble));
printf(" pfloat is %d bytes",sizeof(pfloat));
}
2.
#include<stdio.h>
int main(){
float dval =3.14;
float *pdval; /* pointer to an dval */
pdval=&dval; /*/* store address of dval in pointer variable pdval.Address is stored in hexadecimal format so %x is used*/
printf("Name Address Stored Data ");
printf("dval %x %lf ", &dval,dval);//& is used to print the address of a variable
printf("pdval %x %x ", &pdval,pdval);//pdval stores the address of dval
printf("*pdval %x %lf ", &(*pdval),*pdval);//* is used to return value of the pointer variable
}
3.
#include <stdio.h>
int main()
{
unsigned int array[20], i; //[] used for array declaration .Usigned int so it returns unsigned integer values which include -ve,0,+ve numbers
printf("Elements of Array are ");
for(i = 0; i < 20; ++i)
printf("%d ", *(array + i)); //using pointer to return value *(array+i)
return 0;
}
4.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void BubbleSort(int *array,int size)
{
int i,j;
int temp;
for(i=1;i<size;i++) //Tis loop will compare each element with next element and swap elemets to get elements sorted
{
for(j=0;j<size-i;j++)
{
if(*(array+j)>*(array+j+1))
{
temp=*(array+j);
*(array+j)=*(array+j+1);
*(array+j+1)=temp;
}
}
}
}
void fillArray(int size, int *array)
{
int cnt = 0;
while (cnt < size)
{
array[cnt] = (rand() % 100) + 1;
cnt++;
}
}
void displayArray(int size,int * array){
int i;
for( i=0;i<size;i++)
printf("%d ",array[i]);
}
int main()
{
int array[100];
int i,size;
printf(" Enter the size of array :");
scanf("%d",&size);
srand(time(NULL));
fillArray(size,array);//fillArray function called to fill array elemnets using random function
printf(" Before sorting ");
displayArray(size,array);//display elements in array before sort
BubbleSort(array,size);//calling function by passing the array and its size
printf(" After sorting ");
displayArray(size,array);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.