Programming and Algorithms COMP 1028
9 Questions
2 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

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;
}

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?

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%

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
}

<p>return (testNumber == 1 ? score * 0.1 : testNumber == 2 ? score * 0.2 : testNumber == 3 ? score * 0.2 : testNumber == 4 ? score * 0.15 : testNumber == 5 ? score * 0.15 : testNumber == 6 ? score * 0.2 : 0);</p> 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));
}

<p>7</p> 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]

<p>If A then Do E else Do B If F then Do J else Do G Do D If I then Do J else Do D</p> 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;
}

<p>Line 49: <code>x.next= strlist;</code><br /> Error: <code>x.next</code> has not been allocated memory. Correction: <code>x.next = malloc(sizeof(Node));</code> x.next= strlist; (B), Line 24: <code>strcpy(n1.data, &quot;1028&quot;);</code><br /> Error: <code>n1.data</code> has not been allocated memory. Correction: <code>n1.data = malloc(sizeof(char) * 5);</code> strcpy(n1.data, &quot;1028&quot;); (C), Line 24: <code>strcpy(n1.data, &quot;1028&quot;);</code><br /> Error: <code>n1.data</code> has not been allocated memory. Correction: <code>n1.data = malloc(sizeof(char) * 5);</code> strcpy(n1.data, &quot;1028&quot;); (E)</p> Signup and view all the answers

int mysteryFunction(char *x){
char *marker;
int count = 0;
for (marker = x; *marker; ++marker)
++count;
return count;
}

<p>The function <code>mysteryFunction</code> counts the number of characters in a string.</p> 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;
}

<p>i, sum, z (D)</p> Signup and view all the answers

Flashcards

String

A sequence of characters. In C, often stored in an array. e.g. "Hello"

For loop

A loop that executes a block of code a predetermined number of times. It iterates over a range of values.

While Loop

A loop structure designed to execute a block of code repeatedly as long as a certain condition remains true.

Ending a loop

The act of terminating the execution of a loop.

Signup and view all the flashcards

Switch statement

A programming construct that allows for multiple execution paths based on a condition. It acts like a decision point in the code.

Signup and view all the flashcards

Conditional Expression

A concise way to represent code logic using conditional expressions. It efficiently assigns a value based on the evaluation of a condition.

Signup and view all the flashcards

Pseudocode

A structured approach to break down a problem into smaller, manageable tasks. Typically uses a combination of words and symbols to describe the logic of the problem.

Signup and view all the flashcards

Pointer

A code snippet that refers to the memory location of a variable. It allows you to directly manipulate data in memory.

Signup and view all the flashcards

Linked list

A data structure where data is stored in a linear sequence, with each element pointing to the next.

Signup and view all the flashcards

Scope

The region of a program where a variable is accessible.

Signup and view all the flashcards

Function

A self-contained block of code that performs a specific task, often taking input and returning a value.

Signup and view all the flashcards

Global variable

A variable that is declared and initialized outside of any function, making it accessible across the entire program.

Signup and view all the flashcards

Input

Using standard input to get data from the user in a program.

Signup and view all the flashcards

Output

Displaying information to the user using standard output. On a console, it usually appears as text.

Signup and view all the flashcards

Recursion

A function call that invokes itself within its own definition. It works by breaking down a larger problem into smaller similar subproblems.

Signup and view all the flashcards

Conditional statement

Checking if a condition is true or false, leading to different execution paths in the code.

Signup and view all the flashcards

Loop

Repeating a block of code until a specific condition is met.

Signup and view all the flashcards

Assignment

Storing a value in a memory location identified by a variable name.

Signup and view all the flashcards

Array

A collection of data elements stored in contiguous (next to each other) memory locations and identified by a single variable name.

Signup and view all the flashcards

Local variable

A variable that is declared inside a function or block of code, making it accessible only within its scope.

Signup and view all the flashcards

Boolean

A data type that can store a value that can be evaluated to true or false.

Signup and view all the flashcards

Loop condition

A condition that should be true for a loop to continue executing. When the condition becomes false, the loop ends.

Signup and view all the flashcards

User input

A section of code that is designed to read data from the user and assign it to a variable. It allows the user to provide input to the program.

Signup and view all the flashcards

Pointer variable

A variable that holds a memory address, pointing to the location of another variable in memory.

Signup and view all the flashcards

Modify

To change the value stored in a variable.

Signup and view all the flashcards

Dynamic

A data structure where data is allocated as needed, dynamically. The size of the structure is flexible and changes as needed.

Signup and view all the flashcards

Segmentation Fault

An unexpected error or failure that causes a program to crash or behave abnormally. It usually occurs due to a logical error, a memory-related issue, or a conflict with resources.

Signup and view all the flashcards

Push Node

To add a new node to the front of a linked list.

Signup and view all the flashcards

Breakpoint

A specific point in the code where program execution is paused and control is transferred to a different point.

Signup and view all the flashcards

Calculation Function

A function that calculates a value based on input parameters, and returns a value that represents the result of the calculation.

Signup and view all the flashcards

Function Name

The name of a function that identifies its purpose and provides a reference to the code block.

Signup and view all the flashcards

Loop body

A block of code or statement that is consistently repeated within a loop, each time the loop executes.

Signup and view all the flashcards

Function arguments

The data that is passed to a function as input parameters, allowing the function to access and use information.

Signup and view all the flashcards

Debugging

The process of examining a program for errors and identifying potential areas for improvement. It often involves checking for syntax errors, logical errors, and code efficiency.

Signup and view all the flashcards

Memory error

A common programming error that occurs when a program attempts to access memory or resources that are not allowed, causing a program crash.

Signup and view all the flashcards

Logic error

A type of programming error related to wrong logic or incorrect use of algorithms, leading to unexpected or incorrect results.

Signup and view all the flashcards

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 to for 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.

Quiz Team

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.

More Like This

Recursion in Programming
16 questions

Recursion in Programming

RightfulGoshenite avatar
RightfulGoshenite
Dynamic Programming
20 questions

Dynamic Programming

ChivalrousSmokyQuartz avatar
ChivalrousSmokyQuartz
Use Quizgecko on...
Browser
Browser