0% found this document useful (0 votes)
36 views

Scientific Programming and Computer Architecture 23

This document discusses scientific programming and computer architecture. It summarizes the compilation process where a compiler generates machine instructions for function definitions and places them in memory. It then describes how function calls work, with an example program that uses functions defined in one file (aitken.c) in another file (leibniz.c). The main() function in leibniz.c generates partial sums of the Leibniz series by calling functions like leibniz() and printseq() and the library function aitken().

Uploaded by

Mirsaeid Mouasvi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Scientific Programming and Computer Architecture 23

This document discusses scientific programming and computer architecture. It summarizes the compilation process where a compiler generates machine instructions for function definitions and places them in memory. It then describes how function calls work, with an example program that uses functions defined in one file (aitken.c) in another file (leibniz.c). The main() function in leibniz.c generates partial sums of the Leibniz series by calling functions like leibniz() and printseq() and the library function aitken().

Uploaded by

Mirsaeid Mouasvi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Scientific Programming and Computer Architecture

}
has a totally different effect. When the compiler encounters a function definition, it generates machine
instructions for the body of the function (which is omitted here) and figures out where to place these instructions in
memory. After that, aitken corresponds to the chunk of memory that contains machine instructions that implement
the body of the function. Just as with variables, defining a function amounts to setting aside memory for it during
compilation.

1.2.5  Function calls and the compilation process

Here we take a look at the mechanism of function calls and the compilation process. Much of the discussion is
centered around the file leibniz.c, which uses the functions defined by aitken.c to extrapolate the partial sums
of the Leibniz series and produces data corresponding to table 1.1↑.
The source file leibniz.c begins with two directives:
#include "aitken.h"
#include <stdio.h>
The first directive includes aitken.h to interface to functions defined in aitken.c. The second directive
includes stdio.h to interface to printing functions defined in the stdio (standard input/output) library.
The leibniz() function, which generates partial sums of the Leibniz series, is defined below without
comment.
//partial sums of 4(1-1/3+1/5-1/7+1/9-...)
void leibniz(double* seq, int len){
int i;
for(i=0; i < len; i++)
if(i==0)
seq[i] = 4.0;
else if(i%2==1)
seq[i] = seq[i-1] - 4.0/(2.0*i+1);
else
seq[i] = seq[i-1] + 4.0/(2.0*i+1);
}
The printseq() function prints a sequence using the printf() function defined in the stdio library.

void printseq(double* seq, int len){


int i;
printf("\n \n");
for(i=0; i < len; i++)
printf("%-.10f\n",seq[i]);
}
The logic used by the main() function for generating the data shown in table 1.1↑ is similar to that of
aitkenExtrapolate(). In a C or C++ program, the function named main() is the first to gain control when a
program is run.
1 int main(){
2 const int len = 13;
3 double seq1[len];
4 double seq2[len];
5 int n, i, j;
6 leibniz(seq1, len);
7 n = len/2;
8 if(len%2==0)
9 n--;
10 for(i=0; i < n; i++){
11 printseq(seq1,len-2*i);
12 aitken(seq1, seq2, len-
2*i);
13 for(j=0; j < len-2*

https://divakarvi.github.io/bk-spca/spca.html[20-1-2019 23:44:49]

You might also like