I had to write a program for class that gives first 10 perfect squares, then prints them backwords, then takes a number inputed and gives perfect squares up to that number, then take those and pass them to a function written in another file that is called in main. Every time 100 is printed also print jackpot. these two files need to be compiled using a makefile that combines the seperate function and main.
i have two problems:
1. a 0 prints after the first ten squares. actually its printing before the ten are printed backwords because when i isolate the first part the 0 doesn't print.
2. the part that divides the squares by 9 is rounding like its outputing integers when a rational number is the answer but i am using float i thought in all the right places. also i even tried changin main and all arrays and variables to float and its still wrong. just .0000 is added to the end the correct rational number is not printing.
the code for main, div function and the output are following. please don't be too critical i'm sure there is a much better way to do the parts that work but this is not a C class it's actually a unix class. we have very linited knowledge of C itself. also i'm not sure how to comment out the code for the forum so sorry if thats messed up. its my first post with actual code.
PS: also if someone could tell me the syntax to print every element in an array without a loop that would be great also.
CODE FOR MAIN:
Code:
float main()
{
int number, i, square[32676];
float y[32676];
printf("enter a number to find its square\n");
scanf("%d",&number);
i=1;
while(i<=10)
{
square[i]=i*i;
{
if(square[i]==100)
printf("jackpot!!!");
}
printf("%d\n",square[i]);
i++;
}
while(i>=1)
{
{
if(square[i]==100)
printf("jackpot!!!");
}
printf("%d\n",square[i]);
i--;
}
i++;
while(i<=number)
{
square[i]=i*i;
{
if(square[i]==100)
printf("jackpot!!!");
}
printf("%d\n",square[i]);
y[i]=div9(square[i]);
i++;
}
for(i=1;i<=number;i++)
{
if(y[i]==100)
printf("jackpot!!!");
{
printf("%f\n",y[i]);
}
}
return 0;
}
CODE FOR FUNCTION:
Code:
div9 (int x)
{
(x/9);
return (x/9);
}
OUTPUT:
enter a number to find its square:20
1
4
9
16
25
36
49
64
81
jackpot!!!100
0
jackpot!!!100
81
64
49
36
25
16
9
4
1
1
4
9
16
25
36
49
64
81
jackpot!!!100
121
144
169
196
225
256
289
324
361
400
0.000000
0.000000
1.000000
1.000000
2.000000
4.000000
5.000000
7.000000
9.000000
11.000000
13.000000
16.000000
18.000000
21.000000
25.000000
28.000000
32.000000
36.000000
40.000000
44.000000