You are viewing our Forum Archives. To view or take place in current topics click here.
Help with a C program
Posted:
Help with a C programPosted:
Status: Offline
Joined: Mar 04, 201212Year Member
Posts: 187
Reputation Power: 7
So my struggle is that I have to be able to square root an integer that the user inputs. I know you can use the "sqrt" function from the math.h library but what would be the easier way of using that? My code is as followed:
#include <stdio.h>
#include <stdlib.h>
int main(void){
//Define the variables
int num;
int less = 0;
int between = 1;
int sum = 0;
int count = 0;
long avg;
/*Statements and Loops*/
printf("Please enter random integers, use the number 99999 to stop:");
scanf("%d", &num);
while(num!= 99999){
count++;
sum+= num;
//If statement
//sqrt function from <math.h>
//possible to use a for loop?
//If statement for one number is less than 20
if(num < 20){
less = 1;
}
//If statement for all numbers are between 10 and 90
if(num <= 10 || num >= 90){
between = 0;
}
scanf("%d", &num);
}
avg = sum/count;
//Print out the results
printf("The number of integers is: \t\t%d\n",count);
printf("The sum of the integers is: \t\t%d\n",sum);
printf("The average of the integers is: \t%ld\n", avg);
//If, else to print out true or false for a number being less than 20
if(less == 1){
printf("At least one number was less than 20: \tTrue\n");
}else {
printf("At least one number was less than 20: \tFalse\n");
}
//If, else to print out true or false for all numbers being between 10 and 90
if(between == 1){
printf("All numbers were between 10 and 90: \tTrue\n");
}else {
printf("All numbers were between 10 and 90: \tFalse\n");
}
return 0;
}
#2. Posted:
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
You will want to be able to increase /decrease upper and lower bounds for whatever number you are finding.
This will eliminate numbers either side of the answer and finally reach your actual result.
PS. This was written with my phone so excuse any obvious mistakes.
This will eliminate numbers either side of the answer and finally reach your actual result.
double SquareRoot(double myValue) {
double myPrecision =0.000000001; // the result must match this precision before we take it as correct.
double lowerBound = 0; // start at 0,the lowest possible answer .
double upperBound = myValue; // start at the value, the highest possible answer.
double temp = 0; // initialise our working value.
while (upperBound - lowerBound > myPrecision) { // while we are still finding more precise numbers
temp = lowerBound + (upperBound - lowerBound) / 2; // finding mid value
if (temp * temp > myValue) { // if the value is too large, set temp as our new upper bound.
upperBound = temp;
} else {
lowerBound = temp;
}
}
return temp; // once we have a result with enough precision, return that value.
}
PS. This was written with my phone so excuse any obvious mistakes.
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.