Program must be in C language and MUST be able to run on visual studio 1. Explor
ID: 3721215 • Letter: P
Question
Program must be in C language and MUST be able to run on visual studio
1. Exploring Variables and Memory 1.1 Write a program that: prompts a user to enter a character, an integer, a real number, and an array of five integers o displays each of them and their memory locations in the following form: char value at memory location: 0xhex_number (print the values in place of bold text) int- value at memory location: 0xhex-number (print the values in place of bold text) doublevalue at memory location: 0xhex_number(print the values in place of bold text) int [index] = value at memory location: 0xhex-number (print the values in place of bold text) (Note: hexadecimal numbers in C are denoted by the prefix, 'Ox', to differentiate them from just an alpha-numeric string. Úse the format specifier, %p to print out a memory address.) 1.2 Modify the program in 1.1 to store the char, int and double in a struct. Print the memory location of the struct and the three member variables.Explanation / Answer
/////////////////////program 1.1//////////////////////
#include <stdio.h>
#define TOTAL_ARRAY_ELEMENTS 5
int main()
{
char cVal;
int iVal;
double dVal;
int iArr[TOTAL_ARRAY_ELEMENTS];
int index = 0;
printf("Enter a character value : ");
scanf("%c", &cVal);
printf("Enter a integet value : ");
scanf("%d", &iVal);
printf("Enter a real number : ");
scanf("%lf", &dVal);
printf("Enter 5 numbers in array:- ");
for (index = 0; index < TOTAL_ARRAY_ELEMENTS; index++)
{
printf(" Enter %d number : ", index + 1);
scanf("%d", &iArr[index]);
}
printf("char = %c at memory location: %p ", cVal, &cVal);
printf("int = %d at memory location: %p ", iVal, &iVal);
printf("double = %lf at memory location: %p ", dVal, &dVal);
for (index = 0; index < TOTAL_ARRAY_ELEMENTS; index++)
{
printf("int[%d] = %d at memory location: %p ", index, iArr[index], &iArr[index]);
}
return 0;
}
/////////////////////output 1.1/////////////////////////
hax-vikashar-1$ ./a.out
Enter a character value : E
Enter a integet value : 12343
Enter a real number : 456.23
Enter 5 numbers in array:-
Enter 1 number : 111
Enter 2 number : 222
Enter 3 number : 333
Enter 4 number : 444
Enter 5 number : 555
char = E at memory location: 0x7ffe6b3e6b2b
int = 12343 at memory location: 0x7ffe6b3e6b24
double = 456.230000 at memory location: 0x7ffe6b3e6b18
int[0] = 111 at memory location: 0x7ffe6b3e6b00
int[1] = 222 at memory location: 0x7ffe6b3e6b04
int[2] = 333 at memory location: 0x7ffe6b3e6b08
int[3] = 444 at memory location: 0x7ffe6b3e6b0c
int[4] = 555 at memory location: 0x7ffe6b3e6b10
hax-vikashar-1$
//////////////////////program 1.2/////////////////////////
#include <stdio.h>
#define TOTAL_ARRAY_ELEMENTS 5
int main()
{
struct values
{
char cVal;
int iVal;
double dVal;
}userValues;
int iArr[TOTAL_ARRAY_ELEMENTS];
int index = 0;
printf("Enter a character value : ");
scanf("%c", &userValues.cVal);
printf("Enter a integet value : ");
scanf("%d", &userValues.iVal);
printf("Enter a real number : ");
scanf("%lf", &userValues.dVal);
printf("Enter 5 numbers in array:- ");
for (index = 0; index < TOTAL_ARRAY_ELEMENTS; index++)
{
printf(" Enter %d number : ", index + 1);
scanf("%d", &iArr[index]);
}
printf("char = %c at memory location: %p ", userValues.cVal, &userValues.cVal);
printf("int = %d at memory location: %p ", userValues.iVal, &userValues.iVal);
printf("double = %lf at memory location: %p ", userValues.dVal, &userValues.dVal);
for (index = 0; index < TOTAL_ARRAY_ELEMENTS; index++)
{
printf("int[%d] = %d at memory location: %p ", index, iArr[index], &iArr[index]);
}
return 0;
}
//////////////////////output 1.2////////////////////////////
hax-vikashar-1$ ./a.out
Enter a character value : E
Enter a integet value : 12343
Enter a real number : 456.789
Enter 5 numbers in array:-
Enter 1 number : 555
Enter 2 number : 444
Enter 3 number : 333
Enter 4 number : 222
Enter 5 number : 111
char = E at memory location: 0x7ffc60e2f670
int = 12343 at memory location: 0x7ffc60e2f674
double = 456.789000 at memory location: 0x7ffc60e2f678
int[0] = 555 at memory location: 0x7ffc60e2f650
int[1] = 444 at memory location: 0x7ffc60e2f654
int[2] = 333 at memory location: 0x7ffc60e2f658
int[3] = 222 at memory location: 0x7ffc60e2f65c
int[4] = 111 at memory location: 0x7ffc60e2f660
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.