Chapter 1 Introduction to Computers, the Internet and the Web – 2

1.10 Test-Driving a C Application in Windows, Linux and Mac OS X

In this section, you’ll run and interact with your first C application. You’ll begin by running a guess-the-number game, which randomly picks a number from 1 to 1000 and prompts you to guess it. If your guess is correct, the game ends. If your guess is not correct, the application indicates it’s higher or lower than the correct number. There’s no limit on the number of guesses you can make but you should be able to guess any of the numbers in this range correctly in 10 or fewer tries. There’s some nice computer science behind this game—in Section 6.10, Searching Arrays, you’ll explore the binary search technique.

For this test-drive only, we’ve modified this application from the exercise you’ll be asked to create in Chapter 5. Normally this application randomly selects the correct answers. The modified application uses the same sequence of correct answers every time you execute the program (though the particular sequence may vary by compiler), so you can use the same guesses we use in this section and see the same results.

We’ll demonstrate running a C application using the Windows Command Prompt, a shell on Linux and a Terminal window in Mac OS X. The application runs similarly on all three platforms. After you perform the test-drive for your platform, you can try the randomized version of the game, which we’ve provided with each test drive’s version of the example in a subfolder named randomized_version.

Many development environments are available in which you can compile, build and run C applications, such as GNU C, Dev C++, Microsoft Visual C++, CodeLite, NetBeans, Eclipse, Xcode, etc. Consult your instructor for information on your specific development environment. Most C++ development environments can compile both C and C++ programs.

In the following steps, you’ll run the application and enter various numbers to guess the correct number. The elements and functionality that you see in this application are typical of those you’ll learn to program in this book. We use fonts to distinguish between features you see on the screen (e.g., the Command Prompt) and elements that are not directly related to the screen. We emphasize screen features like titles and menus (e.g., the File menu) in a semibold sans-serif Helvetica font, and to emphasize filenames, text displayed by an application and values you should enter into an application (e.g., GuessNumber or 500) we use a sans-serif Lucida font. As you’ve noticed, the defining occurrence of each key term is set in bold blue type.

For the Windows version of the test drive in this section, we’ve modified the background color of the Command Prompt window to make the Command Prompt windows more readable. To modify the Command Prompt colors on your system, open a Command Prompt by selecting Start > All Programs > Accessories > Command Prompt, then right click the title bar and select Properties. In the “Command Prompt” Properties dialog box that appears, click the Colors tab, and select your preferred text and background colors.

1.10.1 Running a C Application from the Windows Command Prompt

-1. Checking your setup. It’s important to read the Before You Begin section at http://www.deitel.com/books/chtp8/ to make sure that you’ve copied the book’s examples to your hard drive correctly.

2. Locating the completed application. Open a Command Prompt window. To change to the directory for the completed GuessNumber application, type cd C:\examples\ch01\GuessNumber\Windows, then press Enter (Fig. 1.7). The command cd is used to change directories.

Environment : Windows 10 Pro x64 / Visual Studio 2017 (Parallels Desktop)

3. Running the GuessNumber application. Now that you are in the directory that contains the GuessNumber application, type the command GuessNumber (Fig. 1.8) and press Enter. [Note: GuessNumber.exe is the actual name of the application; however, Windows assumes the .exe extension by default.]

4. Entering your first guess. The application displays “Please type your first guess.”, then displays a question mark (?) as a prompt on the next line (Fig. 1.8). At the prompt, enter 500 (Fig. 1.9).

5. Entering another guess. The application displays “Too high. Try again.”, meaning that the value you entered is greater than the number the application chose as the correct guess. So, you should enter a lower number for your next guess. At the prompt, enter 250 (Fig. 1.10). The application again displays “Too high. Try again.”, because the value you entered is still greater than the number that the application chose.

In this section, the author introduce the algorithm called “Binary Search“.
For More Information about “Binary Search”, check this article : https://wordpress.com/block-editor/post/graycs.home.blog/275

6. Entering additional guesses. Continue to play the game by entering values until you guess the correct number. The application will display “Excellent! You guessed the number!” (Fig. 1.11).

I used binary search so that I could guess the number only in 7 steps. (<= 10 [log_2 1000])

7. Playing the game again or exiting the application. After you guess correctly, the application asks if you’d like to play another game (Fig. 1.11). At the prompt, entering 1 causes the application to choose a new number and displays the message “Please type your first guess.” followed by a question-mark prompt (Fig. 1.12), so you can make your first guess in the new game. Entering 2 ends the application and returns you to the application’s directory at the Command Prompt (Fig. 1.13). Each time you execute this application from the beginning (i.e., Step 3), it will choose the same numbers for you to guess.

8. Close the Command Prompt window.

1.10.2 Running a C Application Using GNU C with Linux

For the figures in this section, we use a bold font to point out the user input required by each step. In this test drive, we assume that you know how to copy the examples into your home directory. Please see your instructor if you have any questions regarding copying the files to your Linux system. Also, for the figures in this section, we use a bold font to point out the user input required by each step. The prompt in the shell on our system uses the tilde (~) character to represent the home directory, and each prompt ends with the dollar-sign ($) character. The prompt will vary among Linux systems.

-1. Checking your setup. It’s important to read the Before You Begin section at http://www.deitel.com/books/chtp8/ to make sure that you’ve copied the book’s examples to your hard drive correctly.

2. Locating the completed application. From a Linux shell, change to the completed GuessNumber application directory (Fig. 1.14) by typing

cd examples/ch01/GuessNumber/GNU

then pressing Enter. The command cd is used to change directories.

~$ cd examples/ch01/GuessNumber/GNU ~/examples/ch01/GuessNumber/GNU$

Fig. 1.14 | Changing to the GuessNumber application’s directory.

3. Compiling the GuessNumber application. To run an application on the GNU C++ compiler, you must first compile it by typing

gcc GuessNumber.c -o GuessNumber

as in Fig. 1.15. This command compiles the application. The -o option is followed by the name you’d like the executable file to have—GuessNumber.

Fig. 1.15 | Compiling the GuessNumber application using the gcc command.

4. Running the GuessNumber application. To run the executable file GuessNumber, type ./GuessNumber at the next prompt, then press Enter (Fig. 1.16).

You know the next steps. Just use binary search again.
Environment : Fedora Linux 29 KDE

1.11 Operating Systems

Operating systems are software systems that make using computers more convenient for users, application developers and system administrators. They provide services that allow each application to execute safely, efficiently and concurrently (i.e., in parallel) with other applications. The software that contains the core components of the operating system is the kernel. Popular desktop operating systems include Linux, Windows and Mac OS X. Popular mobile operating systems used in smartphones and tablets include Google’s Android, Apple’s iOS (for its iPhone, iPad and iPod Touch devices), Windows Phone and BlackBerry OS.

1.11.1 Windows—A Proprietary Operating System

In the mid-1980s, Microsoft developed the Windows operating system, consisting of a graphical user interface built on top of DOS—an enormously popular personal-computer operating system that users interacted with by typing commands. Windows borrowed many concepts (such as icons, menus and windows) popularized by early Apple Macintosh operating systems and originally developed by Xerox PARC. Windows 8.1 is Microsoft’s latest operating system—its features include PC and tablet support, a tiles-based user interface, security enhancements, touch-screen and multi-touch support, and more. Windows is a proprietary operating system—it’s controlled by Microsoft exclusively. It’s by far the world’s most widely used operating system.

1.11.2 Linux—An Open-Source Operating System

The Linux operating system—which is popular in servers, personal computers and embedded systems—is perhaps the greatest success of the open-source movement. The open-source software development style departs from the proprietary development style (used, for example, with Microsoft’s Windows and Apple’s Mac OS X). With open-source development, individuals and companies—often worldwide—contribute their efforts in developing, maintaining and evolving software. Anyone can use and customize it for their own purposes, typically at no charge.

Some organizations in the open-source community are the Eclipse Foundation (the Eclipse Integrated Development Environment helps programmers conveniently develop software), the Mozilla Foundation (creators of the Firefox web browser), the Apache Software Foundation (creators of the Apache web server that delivers web pages over the Internet in response to web-browser requests) and GitHub and SourceForge (which provide the tools for managing open-source projects).

Rapid improvements to computing and communications, decreasing costs and open-source software have made it easier and more economical to create software-based businesses now than just a few decades ago. Facebook, which was launched from a college dorm room, was built with open-source software.

A variety of issues—such as Microsoft’s market power, the relatively small number of user-friendly Linux applications and the diversity of Linux distributions (Red Hat Linux, Ubuntu Linux and many others)—have prevented widespread Linux use on desktop computers. But Linux has become extremely popular on servers and in embedded systems, such as Google’s Android-based smartphones.

1.11.3 Apple’s Mac OS X; Apple’s iOS for iPhone ® , iPad ® and iPod Touch ® Devices

Apple, founded in 1976 by Steve Jobs and Steve Wozniak, quickly became a leader in personal computing. In 1979, Jobs and several Apple employees visited Xerox PARC (Palo Alto Research Center) to learn about Xerox’s desktop computer that featured a graphical user interface (GUI). That GUI served as the inspiration for the Apple Macintosh, launched with much fanfare in a memorable Super Bowl ad in 1984.

The Objective-C programming language, created by Brad Cox and Tom Love at Stepstone in the early 1980s, added capabilities for object-oriented programming (OOP) to the C programming language. Steve Jobs left Apple in 1985 and founded NeXT Inc. In 1988, NeXT licensed Objective-C from StepStone and developed an Objective-C compiler and libraries which were used as the platform for the NeXTSTEP operating system’s user interface and Interface Builder—used to construct graphical user interfaces.

Jobs returned to Apple in 1996 when Apple bought NeXT. Apple’s Mac OS X operating system is a descendant of NeXTSTEP. Apple’s proprietary operating system, iOS, is derived from Apple’s Mac OS X and is used in the iPhone, iPad and iPod Touch devices.

1.11.4 Google’s Android

Android—the fastest growing tablet and smartphone operating system—is based on the Linux kernel and Java as its primary programming language. One benefit of developing Android apps is the openness of the platform. The operating system is open source and free.

The Android operating system was developed by Android, Inc., which was acquired by Google in 2005. In 2007, the Open Handset Alliance™—which has 87 company members worldwide—was formed to develop, maintain and evolve Android, driving innovation in mobile technology and improving the user experience while reducing costs. As of April 2013, more than 1.5 million Android devices (smartphones, tablets, etc.) were being activated worldwide daily. Android devices now include smartphones, tablets, e-readers, robots, jet engines, NASA satellites, game consoles, refrigerators, televisions, cameras, health-care devices, smartwatches, automobile in-vehicle infotainment systems (for controlling the radio, GPS, phone calls, thermostat, etc.) and more. Android also executes on desktop and notebook computers.

One thought on “Chapter 1 Introduction to Computers, the Internet and the Web – 2

Leave a comment