CPPS LAB 15 Solutions
CPPS LAB 15 Solutions
Diploma in
AEG/AEL/AMS/BME/BZE/CEN/CER/ELN/
GBS/IMT/INE/IFM/MET/MIE/MTN
Lab Exercise 1
a) Use the ASCII chart to complete the table for the following array declaration if the
contents are interpreted as ASCII characters.
int msg[]={103,110,105,110,114,111,77,32,100,111,111,71,0};
msg[10]
msg[12]
msg[11]
msg[0]
msg[1]
msg[2]
msg[3]
msg[4]
msg[5]
msg[6]
msg[7]
msg[8]
msg[9]
g n i n r o m SP d o o G NUL
b) Write a program to display the character from msg array above character by
character at least once. Your program will run until user press Q to quit.
2. Implementation
Create a Win32
Console Project.
Initialize char [] msg
Add project to current
solution or create new
project and solution
named: Lab15Project at
Prompt and read location C:\Student
input
Add C source file
named lab15.c
Y
Implementation Steps
1. Declare and
initialize array
K=0 and other
variables.
Hints: How many
ways character
(data=msg[k]) array or string
k=0 can be initialized
in C?
N
k++ /*
putchar('\n')
Name: Goh Gwan Gei
Admin No.:1402944A
Class:Y PE11
*/
putchar(data)
#include <stdio.h>
#include <conio.h>
input 'Q'
int msg[]={103,110,105,110,114,111,77,32,100,111,111,71,0};
void main(void) N
{
char input;
int k,data;
do{
printf("Press enter to continue or Q to quit program\n");
input = _getch();
#include <stdio.h>
#include <conio.h>
int msg[]={103,110,105,110,114,111,77,32,100,111,111,71,0};
void main(void)
{
char input;
int k,data;
do{
printf("Press enter to continue or Q to quit program\n");
input = _getch();
printf("The integer value in char will be:\n");
for(k=0;(data=msg[k])!=0;k++)
{
putchar(data);
}
putchar('\n');
}while(input!='Q');
}
1. Problem specification
a. User’s name can consist of any character except ‘*’ and ‘#’.
b. User cannot key in more than 25 characters.
c. User get to see what they key in.
2. Problem Analysis
What are the data inputs to the problem?
None
Input==’*’||Input==’#’
Y
Display ErrorMsg N
Name[j]=inp
ut
j++
j=0
Y
J<MAX&&input!=’\n’
4. Implementation
Create a Win32 Console Project to implement your program.
Add project to current solution or create new project and solution named:
Lab15aProject at location C:\Student
Add C source file named lab15a.c
CPPS (ESE1006) Lab 15: Strings for Information Interchange Page 7
Implementation Steps
1. Declare and initialize array and other variables.
Hints: Character array with maximum size, error message array
/*
Name: Goh Gwan Gei
Admin No.:1402944A
Class: PE11
/*
#include <stdio.h>
#include <conio.h>
#define MAX 25
char welcomeMsg[] = "Please enter your name: (do not include '*' or '#')";
char errorMsg[] = "You have entered a invalid character";
void main(void)
{
int j=0;
char input;
char name[MAX];//character array with Max number
2. Implement loop construct to read in user’s name character by character until user
press <enter> key or reach to maximum number of characters.
Hint: Look out for usage between getche vs getchar. To exit the program
check “less than 25 inputs & not <enter> key”.
else
{
name[j] = input;
j++;
}
} while (j < 25 && input != 13);//less than 25 inputs & not <enter> key
#include <stdio.h>
#include <conio.h>
#define MAX 25
char welcomeMsg[] = "Please enter your name: (do not include '*' or '#')";
char errorMsg[] = "You have entered a invalid character";
void main(void)
{
int j=0;
char input;
char name[MAX];//character array with Max number
do
{
input = _getche();//get input data
if (input == '*' || input == '#')//check for invalid input
{
puts(errorMsg);//display errorMsg[]
j = 0;//reset j
continue;//continue at while()
}
else
{
name[j] = input;
j++;
}
} while (j < 25 && input != 13);//less than 25 inputs & not <enter> key
name[j] = 0;//append a NULL character to end of input
puts("Your name is: \n");
puts(name);//display user's name
}//end program
5. Program Testing
1. Press <Ctrl> <F5> to build and run the program. Enter the required data. Does the
program display the correct answer? Yes it display the correct answer.
2. If the program does not display the correct expected answer, check for errors, make
the changes in the source file and re-run the program by pressing <Ctrl> <F5>.
Write down the errors and reasons during the process of getting your program to
execute correctly.
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
_________________________________________________________________
_________________________________________________________________