Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write C Program: Part a) Complete the runningAveGlobal.c program, which should read integers from the standard input, and computes the running (current) average of the

Write C Program: Part a)

Complete the runningAveGlobal.c program, which should read integers from the standard input, and computes the running (current) average of the input integers. The program terminates when a -1 is entered. Modify the program below, further simplifying communications between functions by using global variables.

Implementation: - named your program runningAveGlobal.c, which contains the main()function.

  • define a function void runningAverage(), which computes the running average in double. Notice that this function takes no arguments and does not return anything.
  • Put the definition of runningAverage() in another file, name the file function.c.
  • define all global variables in function.c

#include

#define MY_PRINT(z) printf("running average is %.3f ",z)

double runningAverage(int);

int main(int argc, char *argv[]) {

int input; int count=0; int sum=0;

double resu;

printf("enter number (-1 to quit): ");

scanf("%d", &input);

while(input != -1) {

resu = r_avg(sum, count);

MY_PRINT(sum, count, resu);

/* read again */

printf("enter number (-1 to quit):

scanf("%d", &input); }

return 0;}

double runningAverage(int currentInput) {

}

sample output:

enter number (-1 to quit): 10

running average is 10 / 1 = 10.000

enter number (-1 to quit): 20

running average is 30 / 2 = 15.000

Part b) Use the program swap.c, shown below and compile using gcc g swap.c. Then invoke gdb by issuing gdb tui a.out. A window with two panels will appear. The upper panel displays the source code and the lower panel allows you to enter commands. Maximize the terminal and use arrow keys to scroll the upper panel so you can see the whole source code. First we want to examine the values of variables mainA and mainB after initialization. So we set a breakpoint at the beginning of line 11 (before line 11 is executed) by issuing break 11. Observe that a B+ symbol appears on the left of line 11. We want to trace the values of variables x and y defined in function swap, both before and after swapping. So we set breakpoints at (the beginning of) line 18 and line 21. Finally we set a breakpoint at line 12 so that we can trace the value of mainA and mainB after the function call. When the program pauses at a breakpoint, you can view the current values of variables with the print or display or even printf command.

#include 
void swap(int, int); 
int main( ) {
 int mainA, mainB;
 mainA = 1;
 mainB = 20000;
 swap(mainA, mainB);
 return 0;} 
void swap(int x, int y){
 int temp = x;
 x = y;
 y = temp;} 

Sample output:

Reading symbols from a.out...done.

(gdb) break 11

Breakpoint 1 at 0x400488: file swap.c, line 11.

(gdb) break 18

Breakpoint 2 at 0x4004a3: file swap.c, line 18.

(gdb) break 21

Breakpoint 3 at 0x4004b5: file swap.c, line 21.

(gdb) break 12

Breakpoint 4 at 0x400497: file swap.c, line 12.

(gdb) run

Starting program: /cos/home/kate/a.out

Breakpoint 1, main () at swap.c:11

(gdb) display mainA

mainA = ?

(gdb) display mainB

mainB = ?

(gdb) continue

Continuing.

Breakpoint 2, swap (x=1, y=20000) at swap.c:18

(gdb) display x

x = ?

(gdb) display y

y = ?

(gdb) display mainA

?

(gdb) display mainB

?

(gdb) continue

Continuing.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

More Books

Students also viewed these Databases questions