Computer Science for Class XII - Programming using C (Solved Exercises/Programs)

Share Embed Donate


Short Description

This e-book has all the solutions for the C exercise programs of Computer Science for Class XII (FBISE)....

Description

Pakistan School and College - Salmiya - Kuwait

Computer Science for Class XII Programming using C Solved Exercises/Programs Raed Bin Shahid

2013

Raed Bin Shahid

2013

Computer Science for Class XII - Programming using C

Table of Contents Introdcution ............................................................................................................................................ 2 Downloading and setting up the compiler ............................................................................................. 2 Downloading and setting up the text editor........................................................................................... 6 Chapter 1 – Introduction to C language (How to use the MinGW/GCC compiler with the Notepad++ text editor) .............................................................................................................................................. 7 Chapter 3 – Loops ................................................................................................................................... 8 Chapter 4 – Choices and Decisions ....................................................................................................... 12 Chapter 5 – Arrays and Strings.............................................................................................................. 17 Chapter 6 – Functions ........................................................................................................................... 24 Chapter 7 – File Management .............................................................................................................. 29

Raed Bin Shahid

Page 1

2013

Computer Science for Class XII - Programming using C Introdcution

I am an ex-student of Pakistan School and College - Salmiya – Kuwait. As I was helping some current students of the school with the C programming exercises, I thought if the work can be compiled into an e-book then everyone else would be able to benefit from it too. Downloading and setting up the compiler and text editor will be explained in here including how to use it and then the solution for the exercises will be given. I thought explaining how to setup the compiler was necessary because schools here are still using the Borland Turbo C++ compiler/IDE (Integrated Development Environment) which is a bit out of date and does not work properly with the new Windows OS’s. For this reason I am going to explain how to setup a popular compiler called MinGW (Minimalist GNU for Windows) which is a Windows port of GNU GCC (GNU Compiler Collection) with a very good and useful text editor for programming called Notepad++. I have strictly used the style and method of the programs written in here similar to those in the textbook as this will be easier for the students to understand the programs if they have gone through the textbook thoroughly. And pardon me if you find any mistakes in here. I have tried my best to keep this e-book error free.

Downloading and setting up the compiler First of all we will have to download the MinGW compiler, and it can be downloaded from here: http://sourceforge.net/projects/mingw/files/

The page will come up with contents which are something like in the image above. Click the link on link at the top (Download mingw-get-inst-20120426.exe (662.7 kB) or something similar as the versions change) to download the compiler. Install the compiler after it has been downloaded. I don’t think I need to explain how to install the compiler as I hope you people should already know that, but I would give some suggestions about it. When it asks you about Repository Catalogues select “Use pre-packaged repository catalogues”, if you have a slow internet connection and don’t want to download the latest repository catalogues. Else select “Download repository catalogues” to download up to date files. Raed Bin Shahid

Page 2

2013

Computer Science for Class XII - Programming using C

When it asks to Select Destination Location leave it as default which will be the root of a directory, “C:\MinGW” in my case. After that when it asks to Select Components, select “C Compiler” only as that’s the only compiler we need.

After the installation is finished we need to add MinGW’s executable files to the system path else we will have a hard time trying to compile the programs. I will break it down into steps and then give Raed Bin Shahid

Page 3

2013

Computer Science for Class XII - Programming using C

screenshots of the process too. These instructions are for Windows 7 and 8 but they should probably be the same for the earlier versions of Windows. 1. Right click the My Computer icon or go to My Computer and then right click on an empty space and click on Properties. 2. Go to Advanced system settings which should be on the top left. 3. Click on the Environment Variables button in the Advanced tab. 4. In the System variables find the Variable called Path and then double click on it. Or just click on it once and then click on the Edit button. 5. Add a semicolon (;) at the end of the Variable value in the text field if it’s already not there. Then add the path to the bin folder which is present is the MinGW installation folder, “C:\MinGW\bin” in my case.

Raed Bin Shahid

Page 4

2013

Computer Science for Class XII - Programming using C

Raed Bin Shahid

Page 5

2013

Computer Science for Class XII - Programming using C

The Path system variable specifies a bunch of directories in Windows. Where if you include one and you are in the Command Prompt and you type a name of a program, it will search these directories for that name of the program. The semicolon separate’s multiple directory paths. Now we will be using the gcc.exe executable to compile our C programs which is present in the bin folder of the MinGW installation folder through the Command Prompt to compile the programs. For this reason we have to add the path location of the bin folder to the system variable path to easily use the gcc.exe all the time by just typing gcc instead of the whole path of file location again and again. Now we will check if the gcc.exe can be run from the Command Prompt. Go to Run (Windows Logo+R is the shortcut to Run) and then type ‘cmd’ in the text field and click ok. The Command Prompt will open, now type ‘gcc’ over there and press enter. If you get something like this, then you followed the steps correctly and the C compiler is ready to compile C programs.

Else if you get “'gcc' is not recognized as an internal or external command, operable program or batch file.” Then you made a mistake while setting up the path and should follow the instruction above and try it again.

Downloading and setting up the text editor You can use the default text editor of Windows (Notepad) to type in the programs. But I recommend you use Notepad++ because it helps a lot while programming. Such as keeping track of open and close braces and every element is distinct as they have different colors. I will not go much in depth about downloading and installing Notepad++ as the procedure is similar to the one that has been explained above. Notepad++ can be downloaded from the following link. http://sourceforge.net/projects/notepad-plus/files/ Install it the same way as showed before. No important changes have to be made during the installation. In the next chapter how to use the compiler and text editor will be shown. Raed Bin Shahid

Page 6

2013

Computer Science for Class XII - Programming using C

Chapter 1 – Introduction to C language (How to use the MinGW/GCC compiler with the Notepad++ text editor) There is only one program in the first chapter. The answer of it is same as the program in the in the textbook under ‘1.3 Basic structure of a program’ on page 7 which prints ‘I Love Pakistan’. I am just going to use this simple program to demonstrate how to use the MinGW compiler and the Notepad++ text editor. Q 6. Write a program that will print the message I Love Computers And compile and run it on the computer. Answer: #include void main(void) { printf("I Love Computers"); } Open the Notepad++ and save new file with any name as C source file.

Now type the C program in the file and then save it.

Now go to the Command Prompt and to the directory where the C source file is saved and then compile the C source file with the gcc.exe application located in the bin folder of the MinGW installation folder which we added in the system path before. The C source file can be compiled with the following syntax. gcc ilc.c –o ilc.exe    

gcc is the MinGW C compiler which we are calling to compile the C source file. ilc.c is the C source file which is to be compiled. –o is the output file flag switch which tells the compiler that the next argument will be the name of the output file. Ilc.exe is the name of the output executable file created from the C source code by the C compiler.

Sometimes it will give a warning while compiling some programs in the text book and not compile the program. You can use the –w swtich while compiling to bypass the warning.

Raed Bin Shahid

Page 7

2013

Computer Science for Class XII - Programming using C

Now compile the C source file with the syntax given above in the Command Prompt and then execute the output file. If the source file has any errors the program will not compile and the debugger will tell what is the error and in which line is it, So that you can debug it. Else the program will compile successfully and you can then execute the output file to run the program. Output:

Chapter 3 – Loops Q 6. What will be printed by the following program? #include void main(void) { int count, total=0; for(count=0;count
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF