Write a program that asks the user to enter an integer with magnitude less than
ID: 3808688 • Letter: W
Question
Write a program that asks the user to enter an integer with magnitude less than 1000. The digits (and sign) of the number should then be divided and stored in a structure that contains variables for a ones digit, tens digit, and hundreds digit, as well as a character variable to store a plus sign '+' if the number is positive or a minus sign '-' if the number is negative. Print the values of the structure. For example, if the structure is called "digit" and the number entered was 245, the structure should contain: digit. sign = '+' digit. hundreds = 2 digit. tens = 4 digit Declare the following structure in a program, along with two instances (s1 and s2) as indicated: struct my_struct {int a; float b;} s1, s2; In the program declare a pointer to the structure. Ask the user to enter the values for each structure. Then, using the pointer, print the values of each structure s1 and s2. Don't access the structure variables directly (i.e. don't use s1.a, s1.b, etc.). Write a program that includes a function (outside of the "main" function) to switch the values of two integer variables using only two pointers as arguments. In the main function, ask the user to enter two numbers. The program should then call the function (using pointers to the variables) to switch the two values and then print the results. Modify the "stack.c" program we looked at in class (available on D2L) such that the variable "top" is a pointer to the "contents" array rather than a regular variable. The pointer should point to the current array element that represents the top of the stack, and should change as values are added and removed. Include a screenshot of the program running ("push" a few values to the stack, "pop" a value or two out of the stack, then display the contents).Explanation / Answer
1)program - magnitude less than 1000:
#include <stdio.h>
struct digitset
{
char sign;
int hundred;
int tens;
int ones;
};
int n;
int hund(int n);
int ten(int n);
int one();
int main()
{
struct digitset digit;
int hundred; int tens; int ones;
printf("enter an integer with magnitude less than 1000 ");
scanf("%d",&n);
//printf("%d",n);
if(n<0)
{
digit.sign = '-';
n=n*(-1);
}
else
digit.sign='+';
hundred= hund(n); digit.hundred=hundred;
tens=ten((n%100));digit.tens=tens;
ones=one(n);digit.ones=ones;
printf(" the digit.sign= %c",digit.sign);
printf(" the digit.hundreds= %d",digit.hundred);
printf(" the digit.tens= %d",digit.tens);
printf(" the digit.ones=%d",digit.ones);
return 0;
}
int hund(int n)
{
while(n!=0)
{
if(n>99)
{
int hund =n/100;
return (hund);
}
else
return(0);
}
}
int ten(int n)
{
while(n!=0){
if( n>9){
int ten= n/10;
return(ten);
}
else
return(0);
}
}
int one(int n)
{
while(n!=0)
{
if(n>0)
n=n%100;
return((n%10));
}
else
return(0);
}
}
2) structure of program- s1 and s2:
#include< stdio.h>
struct my_struct{
int a;
float b;
} ;
int main()
{
struct my_struct *s1, *s2;
s1 = (struct my_struct*) malloc(sizeof(struct my_struct));
s2 = (struct my_struct*) malloc(sizeof(struct my_struct));
printf(" Enter value for structure s1:");
printf(" Enter value of a & b:");
scanf("%d%f",&s1->a,&s1->b);
printf(" Enter value for structure s2:");
printf(" Enter value of a & b:");
scanf("%d%f",&s2->a,&s2->b);
prinft(" values in struture s1:");
printf(" value of a & b: %d%f",s1->a,s1->b);
prinft(" values in struture s2:");
printf(" value of a & b: %d%f",s2->a,s2->b);
return 0;
}
3)call value using pointers:
#include<stdio.h>
void swtich(int *a, int *b)
void main()
{
int x, y;
printf(" the value of x :");
scanf("%d", &x);
printf(" the value of y :");
scanf("%d", &y);
printf(" the value of x and y before swtiching: %d %d", x,y);
swtich(&x, &y);
printf(" the value of x and y after swtiching: %d %d", x,y);
}
void swtich(int *a,int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.