Question

Question 2 (a) Define algorithm and give an example to illustrate it. [5 marks] (b) The following C program codes are given below 1.#include <stdio.h> 2. main() char s 1-Bad; 6 7 char s1 -weather!; char sen [20]; int i, ji for (i-0;i<20: ++i) 10. sen [i]-s [i]; else 12. 13. 14. 15. 16. 17 18. 19 sen [i++ for ( j-o;s1 [j] !-\0 ; j ++ , i++) sen [i]-s1 [j break; 20. 21 22. sen [il-1o puts (sen) Based on the above code segment, answer the following i. What is the output of the program? [3 marks] What will be the output when line 14 is removed from the program? [2 marks] ii. ii. What will be the output when sen [i] in lines 16 and 20 are replaced with sen [j]? [3 marks] iv. What will happen if sen [20] at line 6 is replaced with an array of size 10? [2 marks] v. Rewrite the following segment of code using the while control structure for (j 0;s1 [j] !-0j+, i++) sen [i]-sl [j [5 marks]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.a)

What is Algorithm

An algorithm can be defined in different ways such as

  • It is a set of rules for carrying out calculation either by hand or on a machine
  • It is a finite step – by – step procedure to achieve a required result.
  • It is a sequence of computational steps that transform the input into the output.
  • It is a sequence of operations performed on data that have to be organized in data structures.
  • It is an abstraction of a program to be executed on a physical machine.(Mode of computation)

An algorithm can also be viewed as a tool for solving a well specified computational problem.

In other word we can define the algorithm as "A finite sequence of unambiguous steps terminating in a finite amount of time for all possible inputs of finite size."

Example:

Algorithm to check the number as even or odd

Algorithm CHECK(a)

step-1 : IF A MOD 2 = 0 THEN:

WRITE : "EVEN"

ELSE:

WRITE "ODD"

[END OF IF]

STEP-2 : RETURN

B)

i) The output of the program will be Bad weather!

Explanation: The line number 10 and 11 copies all the characters of s to sen. After copy it will add a space to sen due to line number 14. Again due to line number 15 and 16 all the characters of s1 will append with sen.

Now due to break the loop will terminate

Due to statement of line number 20 sen will turn to a string.

So the statement of line number 21 will print Bad weather!

ii) If we will remove the line number 14 from the program the the output will be : Badweather!

Explanation : If line number 14 will remove from the code so space will not be append to the sen. So output will be Badweather!

iii) When line number 16 and 20 will be replaced by sen[j] then output will be : weather!

Explanation : If we remode the line number 16 and 20 so the code will be sen[j]=s1[j] for j=0 to last character of s1.

So instead of append the characters of s1 will copy from the 0th location of sen. Due to line nubmer 20 a null character will be assigned to last of sen. So out put will be weather!

iv) when sen[20] will replaced with sen[10] then the output will be Bad weather!

Explanation : It should produce only 10 characters but in practically in case of array a number of characters can be stored beyond the limit of array declared, when memory will be free. So output will be Bad weather!

v) The equivalent while loop will be

j=0;

while(s1[j]!='\0')

{

sen[i]= s1[j];

j++;

i++;

}

Add a comment
Know the answer?
Add Answer to:
Question 2 (a) Define algorithm and give an example to illustrate it. [5 marks] (b) The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The following program simulates shuffling a deck of cards. (The line numbers are not ) part of the code). Without changing its functionality, rewrite the program by replacing the 2D array card [5...

    The following program simulates shuffling a deck of cards. (The line numbers are not ) part of the code). Without changing its functionality, rewrite the program by replacing the 2D array card [52] [2] with a double pointer char **card and replacing the code between line 12 and line 26 with a function. The function prototype could be void shuffleCards (char **, char [, char [) 1 7/Shuffling cards 2 4 using namespace std; 6 main() #include #include <cstdlib> <iostream>...

  • Week 1 1 InClass Exercise: Pointer Values Due : Friday March 27, 2020 (8 marks )...

    Week 1 1 InClass Exercise: Pointer Values Due : Friday March 27, 2020 (8 marks ) Indicate what the screen output is for the following code. Show your work on how you determined the values. #include <stdio.h> void MyFunction( char *Msg); int main( void ) { char *pChar, MyString[] = "Hello World" ; pChar = MyString; putchar(pChar[2]); printf( "\n" ); putchar(*(pChar + 5)); printf( "\n" ); pChar = &MyString[3]; puts(pChar); pChar = &MyString[1]; MyFunction(pChar); MyFunction II (pChar); return 0; }...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • C programming Question1 (a) Write a C program that will print out all command line arguments,...

    C programming Question1 (a) Write a C program that will print out all command line arguments, in reverse order, one per line. Prefix each line with its index. 6 marks] (b) Consider this C code snippet int a- 100 int b- 42; inte p- &a; int q-b; p qi printf ("%d %d\n" ,a,*p); When this code is executed, what numbers will it print? [2 marks] (c) Consider this C program int main(int argc,char argv) char* target- "Apple" char vord[100] printf...

  • C++ code... Need help asap QUESTION 2 (20 MARKS) The following program shows a simple example...

    C++ code... Need help asap QUESTION 2 (20 MARKS) The following program shows a simple example to calculate average of three marks. Explain how the program is working as well as data management in your memory. You can illustrates this by using table or drawing. The sample output is illustrated in Figure Q2. 1 #include <iostream> 2 using namespace std 3 float calculateAverage (int* marks) int sum=0; float average, for (int İzd ;ia;i++) sumesum+( (marks+i)12 10 11 average float (sum)...

  • OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main()...

    OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main() int mptr, *cptr mptr = (int*)malloc(sizeof(int)); printf("%d", "mptr); cptr = (int)calloc(sizeof(int),1); printf("%d","cptr); garbage 0 000 O garbage segmentation fault Question 8 1 pts If this program "Hello" is run from the command line as "Hello 12 3" what is the output? (char '1'is ascii value 49. char '2' is ascii value 50 char'3' is ascii value 51): #include<stdio.h> int main (int argc, char*argv[]) int...

  • Store information of 5 students using Structure Given details (like roll number, name and marks) of...

    Store information of 5 students using Structure Given details (like roll number, name and marks) of 5 students as an input, read all the details into Student structure and display the content. Input: 1 Jack 72.12 2 Tom 42.42 3 Robert 72.12 4 Michael 72.12 5 James 72.12         where: All lines represent Roll number, Name and Marks respectively. Output: 1 Jack 72.12 2 Tom 42.42 3 Robert 72.12 4 Michael 72.12 5 James 72.12 not sure what to do...

  • there were no solutions to this past paper question. Question 1 (24 marks]. (a) Rewrite the...

    there were no solutions to this past paper question. Question 1 (24 marks]. (a) Rewrite the following linear program in standard inequality form: minimise 78 - 12 +503 - 1034 subject to -11x1 - 12.12 - 13 + 14 > 1, 11-472-873 = 12 21 +672 +31, 57. 11,1220, 13 50 I, unrestricted (b) Consider the following linear program in standard equation form: maximise + 212-383 + 70s subject to I +212 +2.13 + 14 = 3 I + 2x2...

  • Question 2 (a) Define the term "surge impedance loading". [2 marks] (b) Prove that when the...

    Question 2 (a) Define the term "surge impedance loading". [2 marks] (b) Prove that when the loss less line delivers load equal to the surge impedance loading voltages at both ends of the line are equal in magnitude. [4 marks) (c) A 400 kV, 50 Hz, 300 km transmission line delivers 700 MW at 0.85 power factor lagging. Parameters of the line are given below; Series impedance : z=(0.025+j0.3) 22/km Shunt admittance : y=(j4x106) s/km i. If the voltage at...

  • Question No.1 [CLO 1][7 marks] 1. Consider the following pseudocode: Algorithm IterativeFunction (a, b) // a...

    Question No.1 [CLO 1][7 marks] 1. Consider the following pseudocode: Algorithm IterativeFunction (a, b) // a and b are integers while (a>0) B- a/2 A a-2 end while return b; i. What is the time complexity of the IterativeFunction pseudocode shown above? ii. What is the space complexity of the IterativeFunction pseudocode shown above? 2. What is the time complexity of the following algorithm (Note that n(n+1) 2,2 n(n+1)(2n+1) 2 and ): Provide both T(n) and order, e(f(n)). int A=0;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT