Podcast
Questions and Answers
Convert the following while
loop into a for
loop.
#include < stdio.h >
int main() {
int sum = 0, i = 3;
while (i < 10) {
i++;
if (i % 3 == 0)
sum += i;
}
return 0;
}
Convert the following while
loop into a for
loop.
#include < stdio.h >
int main() {
int sum = 0, i = 3;
while (i < 10) {
i++;
if (i % 3 == 0)
sum += i;
}
return 0;
}
for (int i = 3; i < 10; i++) { if (i % 3 == 0) sum += i; }
What reserved word can be used to end an execution of a loop?
What reserved word can be used to end an execution of a loop?
break
Complete the program below by using switch
statement, to compute and print the appropriate value of weightedScore
using the weightings given in the following table.
#include < stdio.h >
int main() {
int testNumber, score;
float weight = 0.0;
printf("Please enter the test number and the score: ");
scanf("%d%d", & testNumber, & score);
//complete your switch statement here
printf("A score of %d on test %d give a weighted score of %.2f\n", score, testNumber, weight);
return 0;
}
Test Number
Weight
1
10%
2
20%
3
20%
4
15%
5
15%
6
20%
Complete the program below by using switch
statement, to compute and print the appropriate value of weightedScore
using the weightings given in the following table.
#include < stdio.h >
int main() {
int testNumber, score;
float weight = 0.0;
printf("Please enter the test number and the score: ");
scanf("%d%d", & testNumber, & score);
//complete your switch statement here
printf("A score of %d on test %d give a weighted score of %.2f\n", score, testNumber, weight);
return 0;
}
Test Number | Weight |
---|---|
1 | 10% |
2 | 20% |
3 | 20% |
4 | 15% |
5 | 15% |
6 | 20% |
switch (testNumber) { case 1: weight = score * 0.1; break; case 2: weight = score * 0.2; break; case 3: weight = score * 0.2; break; case 4: weight = score * 0.15; break; case 5: weight = score * 0.15; break; case 6: weight = score * 0.2; break; default: printf("Invalid test number\n"); }
Write a function that solves the same problem described in part ii) using only conditional expressions.
The whole body of the function should be written using only one statement.
The following C program will display the following output for an input of 3 and 27:
A score of 27 on test 3 gives a weighted score of 5.4.
#include < stdio.h >
float result(int testNumber, int score); //function prototype
int main() {
int testNumber, score;
float weight = 0.0;
printf("Please enter the test number and the score: ");
scanf("%d%d", & testNumber, & score);
weight = result(testNumber, score);
printf("A score of %d on test %d give a weighted score of %.2f\n", score, testNumber, weight);
return 0;
}
float result(int testNumber, int score) {
//write your function
}
Write a function that solves the same problem described in part ii) using only conditional expressions.
The whole body of the function should be written using only one statement.
The following C program will display the following output for an input of 3 and 27:
A score of 27 on test 3 gives a weighted score of 5.4.
#include < stdio.h >
float result(int testNumber, int score); //function prototype
int main() {
int testNumber, score;
float weight = 0.0;
printf("Please enter the test number and the score: ");
scanf("%d%d", & testNumber, & score);
weight = result(testNumber, score);
printf("A score of %d on test %d give a weighted score of %.2f\n", score, testNumber, weight);
return 0;
}
float result(int testNumber, int score) {
//write your function
}
Signup and view all the answers
#include <stdio.h>
int inclusion(int n1, int n2);
int mystery(int n);
int main(){
int n;
printf("%d", mystery(n));
return 0;
}
int inclusion(int n1, int n2){
if (n1 == 0)
return 0;
else
return n2 + inclusion(n1 - 1, n2);
}
int mystery(int n){
if (n == 0)
return 1;
else
return inclusion(2, mystery(n - 1));
}
#include <stdio.h>
int inclusion(int n1, int n2);
int mystery(int n);
int main(){
int n;
printf("%d", mystery(n));
return 0;
}
int inclusion(int n1, int n2){
if (n1 == 0)
return 0;
else
return n2 + inclusion(n1 - 1, n2);
}
int mystery(int n){
if (n == 0)
return 1;
else
return inclusion(2, mystery(n - 1));
}
Signup and view all the answers
flowchart LR
subgraph A
A[A?] -->|YES| E
A -->|NO| B
end
B --> F
subgraph F
F[F?] -->|YES| J
F -->|NO| G
end
G --> D
subgraph D
D[D?] -->|YES| J
D -->|NO| D
end
J[Do J]
E[Do E]
D[Do D]
G[Do G]
B[Do B]
flowchart LR
subgraph A
A[A?] -->|YES| E
A -->|NO| B
end
B --> F
subgraph F
F[F?] -->|YES| J
F -->|NO| G
end
G --> D
subgraph D
D[D?] -->|YES| J
D -->|NO| D
end
J[Do J]
E[Do E]
D[Do D]
G[Do G]
B[Do B]
Signup and view all the answers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// linked list node for a list of c-strings
typedef struct node {
char *data; //string data in this node
struct node *next; //next node or NULL if none
} Node;
// print strings in list that starts at head
void print(Node *head); //function prototype print
// add x to front of strlist and return pointer to new list head
Node *push_node(Node x, Node *strlist);
//function prototype push_node
// link two nodes together as a list and then print the list
int main () {
Node n1;
Node n2;
Node *list = NULL;
// copy "1028" to first node and push onto front of list
strcpy(n1.data, "1028");
list = push_node(n1, list);
// copy "COMP" to second node and push onto front of list
strcpy(n2.data, "COMP");
list = push_node(n2, list);
// print list
print(list);
return 0;
}
// print strings in list that starts at head
void print(Node *head) {
Node *p = head;
while (p != NULL) {
printf("%s", p->data);
p = p->next;
}
}
// add x to front of strlist and return pointer to new list head
Node *push_node(Node x, Node *strlist) {
x.next= strlist;
return &x;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// linked list node for a list of c-strings
typedef struct node {
char *data; //string data in this node
struct node *next; //next node or NULL if none
} Node;
// print strings in list that starts at head
void print(Node *head); //function prototype print
// add x to front of strlist and return pointer to new list head
Node *push_node(Node x, Node *strlist);
//function prototype push_node
// link two nodes together as a list and then print the list
int main () {
Node n1;
Node n2;
Node *list = NULL;
// copy "1028" to first node and push onto front of list
strcpy(n1.data, "1028");
list = push_node(n1, list);
// copy "COMP" to second node and push onto front of list
strcpy(n2.data, "COMP");
list = push_node(n2, list);
// print list
print(list);
return 0;
}
// print strings in list that starts at head
void print(Node *head) {
Node *p = head;
while (p != NULL) {
printf("%s", p->data);
p = p->next;
}
}
// add x to front of strlist and return pointer to new list head
Node *push_node(Node x, Node *strlist) {
x.next= strlist;
return &x;
}
Signup and view all the answers
int mysteryFunction(char *x){
char *marker;
int count = 0;
for (marker = x; *marker; ++marker)
++count;
return count;
}
int mysteryFunction(char *x){
char *marker;
int count = 0;
for (marker = x; *marker; ++marker)
++count;
return count;
}
Signup and view all the answers
#include <stdio.h>
char *username = "UNMC";
double average( int *x, int n);
int main(){
int z[5];
int i;
for(i=0;i<5;i++)
z[i] = i;
printf("%.2lf\n",average(z,5));
double average( int *x, int n) {
int sum=0;
int i;
for(i=0;i<n;i++)
sum = sum + x[i];
return (double)sum/n;
}
#include <stdio.h>
char *username = "UNMC";
double average( int *x, int n);
int main(){
int z[5];
int i;
for(i=0;i<5;i++)
z[i] = i;
printf("%.2lf\n",average(z,5));
double average( int *x, int n) {
int sum=0;
int i;
for(i=0;i<n;i++)
sum = sum + x[i];
return (double)sum/n;
}
Signup and view all the answers
Study Notes
Examination Information
- Examination Title: Programming and Algorithms (COMP 1028)
- Semester: Autumn 2018-19
- Level: 1
- Duration: One hour
- Allowed Materials: Dictionaries, provided one is permitted. For example, a standard translation dictionary if the subject of the examination is not your native language. Electronic devices incapable of storing text are not permitted.
- Instructions: Candidates must not start writing until instructed. Answer all questions. No calculators permitted.
- Invigilator Instructions: Collect all exam papers and answer booklets at the end of the exam.
Programming and Algorithm Topics
-
Program control: Converting
while
loops tofor
loops -
Reserved words: Keywords used to end loops (e.g.,
break
) - Recursion: This refers to a function calling itself.
- Pseudocode: This is an informal description of steps in a program. This uses natural language like English to express the algorithms in a systematic way.
-
Weighted scores: Calculating weighted scores for different test numbers, using a
switch
statement for specific weighting cases -
Conditional expressions: A function using only conditional expressions to solve a problem previously described using a
switch
statement. -
Function prototype: Used for defining functions before they are called like
float result(int testNumber, int score);
Linked List, pointers, scope, and functions
- Linked list nodes: A data structure to store related data items/characters, in a chain-like fashion.
- Pointers: Used to store memory addresses in a program.
- Scope: The region of a program where a variable is accessible.
- C functions: self-contained blocks of instructions designed to perform specific tasks within a program
-
Error detection/correction: Addressing errors in C codes and providing solutions for
segmentation faults
within linked list functions. - Local variables: Variables declared within the local scope of a function
- Global Variables: Variables declared outside any function
Data Structures
- Array: This is a data structure used for storing multiple values (in order) in a contiguous area of memory
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Prepare for the COMP 1028 examination on Programming and Algorithms. This quiz covers key topics including program control, recursion, and the use of pseudocode. Ensure you understand how to convert loops and utilize reserved words effectively.