Write a C program that asks the user to enter a positive integer (the integer co
ID: 3670141 • Letter: W
Question
Write a C program that asks the user to enter a positive integer (the integer could be of any number of digits in the range of the integer type) and replace each digit by the sum of that digit plus 6 modulus 10. The program then should swap the first digit with the last digit before it displays the output. A sample input/output: Enter the number of digits of the number: 5 Enter the number: 92828 Output: 48485 1) Name your program replace2.c. 2) The user will enter the total number of digits before entering the number. 3) You can use format specifier "%1d" in scanf to read in a single digit into a variable (or an array element). For example, for input 101011, scanf("%1d", &num) will read in 1 to num. 4) As part of the solution, write and call the function replace() with the following prototype. The replace() function assumes that the digits are stored in the array a and computes the replaced digits and store them in the array b. c represents the size of the arrays. void replace(int *a, int *b, int n); The replace() function should use pointer arithmetic – not subscripting – to visit array elements. In other words, eliminate the loop index variables and all use of the [] operator in the function. 5) As part of the solution, write and call the function swap() with the following prototype. void swap(int *p, int *q); When passed the addresses of two variables, the swap() function should exchange the values of the variables: swap(&i, &j); /* exchange values of i and j */
attached to be modified:
#include <stdio.h>
void edge(int n, int a[], int b[]);int main(void){
int input[8]={0};int output[8];
int i;printf("please enter the 8 bit bar code");for(i=0;i<8;i++)
scanf("%1d", &input[i]);
edge(8, input, output);for(i=0;i<8;i++)
printf("%d", output[i]);
return 0;
}
void edge(int n, int a[], int b[]){
int i;b[0]=0;for (i = 1;i<n; i++)if(a[i]==a[i-1])b[i]=0;
else
b[i]=1;
}
Explanation / Answer
#include <stdio.h>
//void edge(int n, int a[], int b[]);
void replace(int *a, int *b, int n);
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b= temp;
}
int main(){
int n;
//int input[8]={0};int output[8];
printf("enter the total number of digits");
scanf("%d",&n);
int input[n],output[n];
int i;
printf("please enter the number");
for(i=0;i<n;i++)
scanf("%1d", &input[i]);
replace(input,output,n);
for(i=0;i<n;i++)
printf("%d", output[i]);
return 0;
}
void replace(int a[], int b[], int n){
int i;
//b[0]=0;
for (i = 0;i<n; i++)
*(b+i) = (*(a+i)+6)%10;
swap(b,b+4);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.