Write an C language function named computeMagicNumber which will accept a string
ID: 3728872 • Letter: W
Question
Write an C language function named computeMagicNumber which will accept a string (name of an individual) as input and compute a magic number from this argument using the following formula.
In the above formula, ascii(name[i]) will return the ascii equivalent of a letter.
There is no need for implementing this function in assembly since characters in a string are already represented in ascii.
% is the remainder operation.
name is the string argument passed to the function.
N-1 MagicNun ber-C2(1+1)x(ascii(nameliD)%1001 (it 1) ×lascii(nameBDI 1961001 i 0Explanation / Answer
C Program: Find computeMagicNumber() function in below code.
#include<stdio.h>
int ascii(char c)
{
return (int)c;
}
int computeMagicNumber(char* name)
{
int sum, i; //varibales
sum = 0; //set sum to 0
//calculate sum
int N = sizeof(name)/sizeof(name[0]); //get name size
for(i=0;i<N;i++)
sum = sum + (i+1)*ascii(name[i]);
return sum%1001; //return magic number
}
int main()
{
char name[100] = "Student";
printf("Magic Number is %d ", computeMagicNumber(name));
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.