Computer Science Assignment
Computer Science Assignment
1i) the best place to declare and define global variables would be outside the main() function
Char: the data type accepts single character values from the alphabet
1iii) A variable is a value that is going to change during program execution. Variables are declared by
giving them a data type and initializing them
1iv) a local variable is defined inside a function and can only be accessed within that function, whereas a
global variable can be accessed throughout the whole program and isn’t defined within a function.
This is a style of programming characterized by the recognition of the classes of objects linked to the
functions that they are associated with. Features of object-oriented programming include inheritance,
encapsulation, abstraction and polymorphism
3b) Interpreter
An interpreter is a translator. It translates instructions into machine code one instruction at a time. The
CPU executes each instruction before the interpreter moves on to translate the next instruction.
4a) Traditional programming refers to the manual process of writing a program using a programming
language that uses a syntax of statements that are closer to English and are easily understood by a
programmer and machine language is a low-level language that is made up of binary numbers (zeros
and ones) and can only be understood by the computer.
4b) Explicit data conversion is a process of passing data to the compiler that the program is trying to
perform a conversion with the knowledge of s a possible data loss whereeas implicit data conversion is
when a smaller data type is converted to a larger data type or when a derived class is converted into a
base class and no data loss happens during this conversion
5a) Compiler Advantages
Speed: Compilers run faster than interpreters because it is a precompiled package that is executed at
the end.
Data Security: Files produced by a compiler can be run by any system without need for the source code,
this makes the program secure and unhackable as users will not have access to the source code.
Debugging is easier as only one line of code is translated and executed at a time.
They are more memory efficient as no temporary storage of translated code takes place as codes are
translated and executed as the interpreter then moves on the the next line of code
6a) For the text, ‘I am a student’ I would use a one dimensional character array
6b) For the number 204.78 I would use the float data type because it accepts numbers with a decimal
point
6c) For the letter ‘A’ I would use the char data type because it stores a single character and only requires
one byte of memory
7a) Structures
#include <stdio.h>
#include <string.h>
struct Car {
char make[20];
char model[10];
int year;
float miles;
} car1;
int main() {
strcpy(car1.model, "C200");
car1.year= 2016;
car1.miles = 25000.89;
// print struct variables
return 0;