Page 3 of 5
Question 3
(20 marks, each part is 5 marks)
For the following snippets, how many times is the printf statement
executed? Briefly explain (up to 3 sentences).
Part a
int i, j;
int n = 100;
for (i = 1; i <= n; i++) {
for (j = 3*i; j <= n; j++) {
printf("programming is fun\n");
}
}
Part b
int i, j;
int n = 1000000;
for (i = 1; i <= n; i++) {
for (j = 1; j <= 10000; j++) {
printf("%d %d\n", i, j);
}
}
Part c
int i = 0;
int n = 10;
int j;
while (i < n) {
i++;
j = i;
while (i < n) {
printf("hello %d\n", i);
i++;
}
i = j;
}
Part d
int i = 0;
int n = 10;
int j;
while (i < n) {
i++;
j = i;
while (i < n) {
printf("hello %d\n", i);
i++;
break;
}
i = j;
}
Please Answer In C
part A:
printf statement=1650
because for (i = 1; i <= n; i++)
it shows i until it gets 100 chance
for (j = 3*i; j <= n; j++)
then i values are multiple of 3
then 3 multiple of each i value
example j=3*i =3 *9
it will that many times of execute
part B:
printf statement = 9521
because it execute according to statement
for (i = 1; i <= n; i++) {
for (j = 1; j <= 10000; j++) {
printf("%d %d\n", i, j);
}
}
it will excute i and j values of times
part C:
printf statement = 45
because
n value 10
while (i < n) {
i++; ///////// it will increase one by one value example i+1
j = i;
while (i < n) {
printf("hello %d\n", i);
i++;
}
i = j; // it will equal to j until it get end of calculation
}
part D:
printf statement = 9
int j;
while (i < n) {
i++;
j = i;
while (i < n) {
printf("hello %d\n", i);
i++;
break;///////// in upper program it will go continouse work but in this statement will stop when i process over
}
i = j;
}
Page 3 of 5 Question 3 (20 marks, each part is 5 marks) For the following...
For the following parts, try to get the best Big-O estimate that you can and briefly justify your answers. Part a) int i, j; int n = 100; for (i = 1; i <= n; i++) { for (j = 3*i; j <= n; j++) { printf("programming is fun\n"); } } Part b) int i, j; int n = 1000000; for (i = 1; i <= n; i++) { for (j = 1; j <= 10000; j++) { printf("%d %d\n",...
For the following parts, try to get the best Big-O estimate that you can and briefly justify your answers (3-4 sentences per part). You should also consider running times for all the operations contained within the loop body (but ignore the running times for initializer, entry condition and increment). Part a int i, j; int n = 100 ; for (i = 1 ; i <= n; i++) { for ( j = 3 *i ; j <= n; j++)...
Please answer the above queston (in terms of C programming) and
explain answer
Semester 2 & Trimester 3B, 2015 COMP1004 Engineering Programming Question 3 - Repetition (20 marks) Q3(a) What will be printed when the following code is executed? (5 marks] #include <stdio.h> int main() int i, j; for(i=1;i<17;i=i+2) { printf("%d==", i); for(j=i; j>0;j--) printf("#"); printf("\n"); return 0; == # 3 = = # # # 5 == ### ## 7 #### #
For each of the below code snippets, identify the bounding function (the big O) of the number of times the indicated line is run (justify your answer briefly): int i = 1: while (i < n) { printf ("Insert difficult work here!") i = i + i: } for(i = 0: i < n: i++) { for (j = 0: j < n: j++) { for (k = 0: k < n: k++) { if(i==j && j==k) arr[i] [j] [k]...
PART FOUR:(20 points) Predict the output that would be shown in the terminal window when the following program fragments are executed 1. Assume that an int variable takes 4 bytes and a char variable takes 1 byte #include<stdio.h> int main() int A[]-{10, 20, 30, 40); int *ptrl - A; int ptr2 = A +5; printf("The Resull: %d\n".(ptr2-ptrl)); printf("The Resul2: %d\n".(char*)ptr2-(char*)ptr); return 0; 2. What does the following program print? #include <stdio.h> int main(void) unsigned int x 1: unsigned int total...
Q4) what is the output after the code below is executed? #include <stdio.h> #1 nclude<string.h> int main) char abc[100]-"Good Luck in Your Programming Final" int i, ; while(abc[i]!= '\0'){ if(abc[i]' if(abc[i+1]-'&&abci+1] -'0' else printf"c", abcli-1]) printf("\n w = %d return 0; \n", w); Place you answer here 4 l Page
10 What does the last printf in this code print to the screen? a)n-7 b)n- 0 c)n--1 d) None of the above 鬐include< stdio. h> int main) int n-0 for (n-7: n<O; n-- printf (n") printf ("n *%d", n); - return 11-What does the code print to the screen? 鬐include< stdio.h> void fun (int) int main) b) 8 d) None of the above fun (3) return void fun (int a) int x-10 while(a !_ 3 x > s} if a...
/* • The following code consists of 5 parts. • Find the errors for each part and fix them. • Explain the errors using comments on top of each part. */ #include <stdio.h> #include <string.h> int main( ) { ////////////////////////////////////////////////////////////////////////// ////////////// Part A. (5 points) ////////////////// int g(void) { printf("%s", Inside function g\ n " ); int h(void) { printf(" % s ", Inside function h\ n "); } } printf("Part A: \n "); g(); printf(" \n"); ////////////////////////////////////////////////////////////////////////// ////////////// ...
Create a UNIX makefile for the following C
program:
//convert.c
char toUpper(char c){
if(c>='a' && c<='z')
return c-32;
return c;
}
//converting sentance to upper
void convertSentence(char *sentence){
int i=0;
while(sentence[i]!='\0'){
//convert to upper for each character
sentence[i] = toUpper(sentence[i]);
i++;
}
}
//converting all sentences into uppercase
void convertAll(char **sentenceList, int numOfSentences){
int i=0;
while(i<numOfSentences){
//calling convertsentence function to conver uppercase
convertSentence(sentenceList[i]);
i++;
}
}
sentences.c
#include<stdio.h>
#include<stdlib.h>
#include "convert.c"
int main(){
//declaring character array
char **mySentences;
int noOfSentences;...
This code should be Runnable on MARS (MIPS Assembler and Runtime Simulator) IDE Convert the following c code into mips #include <stdio.h> #include <string.h> char cipherText[200] = "anything"; int countNumberOfCharInCipher(char*); int countNumberOfCharInCipher(char* cText) { return strlen(cText); } int countSpaces(int numberOfChar, char input[]) { int spaceCounter =0; for(int i=0; i < numberOfChar; i++) { if(input[i] == 32) spaceCounter++; } return spaceCounter; } void decrypt(int numberOfChar, int key, char * cipherText, char * plainText) { int j = 0; int i =0;...