Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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 0

Explanation / 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;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote