Unit 1 Flashcards
(14 cards)
How do you include a catalog ?
include <stdio.h></stdio.h>
#include <math.h></math.h>
How do we define variables?
int x,y;
float x,y;
or
int x=3, y=9;
How do we start our main set of functions ? and what does the “int” stand for.
int main() {
return 0;
}
“int” means that we will output an integer
Write a “for” statement that prints the sequence of 1 to 50
int i;
for (i=1; i<=50; i++)
printf(“%d\n”, i);
return 0;
Write a “for” statement that prints a sequence of 0 to 50 by intervals of 10
int i, count=0;
for(i=1; i<6; i++)
{
count = i*10;
printf(“%d\n”, count);
}
return 0;
Write a program that tells whether or not the number you entered is a multiple of 5
include <stdio.h></stdio.h>
#include <math.h></math.h>
int main () {
int x;
printf(“Enter a multiple of 5: “);
scanf(“%d”, &x);
if (x%5==0)
printf(“Yes”);
else printf(“NO”);
return 0;
}
Explain these line of code if given a set of numbers:
for(i=0; i<=500; i++){
if (a[i]>150.0 && a[i]<200.0){
count ++;
}
}
printf(“%d\n”, count);
return 0;
line 1: this is a line to count i to 500 by intervals of 1
line 2: starting an if statement for the condition that a[] picks up a number between the values 150 & 200
line 3: the {} brakets define the condition; under this condition the variable count increases by and interval of 1 every time it crosses a number that meets the condition os line 2.
line 4 the brackets close out both the for and if statements.
include <stdio.h></stdio.h>
How to ensure this code produces floats:
#include <stdlib.h></stdlib.h>
int main()
{
int i;
for (i=0; i < 10; i++) printf(“%d\n”, rand());
printf(“\nMAX = %d\n”, RAND_MAX);
return 0;
}
change the %d to %f then multipy the “rand” part by a floating 1: 1.0 x rand()
include <stdio.h></stdio.h>
Explain what comes out and why
void func1(float x, float y)
{
x = -x + y;
y = y -1;
}
void func2(float *px, float *py)
{
*px = *py - *px;
*py = 2**px + *py - 1;
}
int main()
{
float x=2, y=3, z= 1;
int i;
func1(x, y);
func2(&x, &y);
func2(&y, &x);
printf(“%f\n”, y);
return 0;
}
-3 is the answer
we work with the func2’s since it is the last one mentioned and also one of the two functions out of the three that actually has the values addressed.
We can then do the math starting with the first equation:
Since its Func2(y, x) our y value will be put in place of x for the solution.
*px = 1 - 3 = -2
Now the second equation:
*py = 2(-2) + (2) -1
= -4 + 1
*py = -3
include <stdio.h></stdio.h>
Explain the Output:
int main()
{
int i;
float x=1;
for (i=10;i<13;i++)
x = (x + 6/x)/2;
printf(“%f\n”, x);
return 0;
}
since there is a For Loop we know that it will run until the value of i is just before 13.
10,11,12 = 3 iterations.
we solve the equation 3 time adding the new value of x to each time.
What does *a *(a+i) mean
this is calling into the array a but based on the number being added
a[i]
how to declare a pointer using p
pa
how to declare a pointed directly onto a variable
&a
how to make a pointer spit our a variable ?
*pa
*&a