I have to use:
float average(const int tab[], int size);
float stdDev(const int tab[], int size);
to printf average and stdDev in C.
I have problem with average and i think with const int.
When i add const int tab[101] i have error with a1;
So how can i make it work with const int (if i can).
And if it is anything wrong with this code.
Any help will be helpful.
#include<stdio.h>
#include<math.h>
float average(const int tab[], int size);
float stdDev(const int tab[], int size);
int main()
{
float ave, std;
int a1;
int j;
int tab[101];
printf("Podaj liczby: ");
for(j=0; j<=99; j++)
{
a1 = scanf("%d", &tab[j]);
if(a1<1)
{
printf("Incorrect input");
return 1;
}
if(tab[0]==0)
{
printf("not enough data available");
return 2;
}
if(tab[j]==0)
{
break;
}
}
ave = average(tab, j);
printf("%.2f\n", ave);
std = stdDev(tab, j);
printf("%.2f", std);
return 0;
}
float average(const int tab[], int size)
{
int i;
float y=0, x;
if(size<=0)
{
return -1;
}
for(i=0; i<size; i++)
{
x = x + tab[i];
}
y = x/size;
return y;
}
float stdDev(const int tab[], int size)
{
int i;
float y, z, z1, z2=0, z3=0;
if(size<=0)
{
return -1;
}
y = average(tab, size);
for(i=0; i<size; i++)
{
z = tab[i] - y;
z1 = pow(z, 2);
z2 = z2 + z1;
z=0;
z1=0;
}
z3 = sqrt(z2/size);
return z3;
}
You define the variable x in average here:
float y=0, x;
without giving it a value. Then here:
x = x + tab[i];
you are reading its value without setting it anywhere beforehand. Because you never gave x a value, its value will be indeterminate and reading it will cause undefined behavior, which means that your program could e.g. print garbage output.
Always initialize your variables:
float y=0, x=0;
Related
Calculating the sum of the first k numbers of the sequence a[0] = 1, a[k] = k*a[k-1] +1/k ( k = 1, 2, ... ).
UPD
There is still a problem with the recursive function ...What is the error?
#include <stdio.h>
#include <stdlib.h>
float m(float n){
float k=1;
float sum=k;
int i;
for (i=1; i<n;i++){
k = (i*k+1.0/i);
sum = sum+k;
}
return sum;
}
float Fn(float n)
{
if (n==0) {
return 1;}
return ((n*Fn(n-1)+1.0/n)+Fn(n-1));
}
int main(int argc, char *argv[]) {
float k;
printf("input k : ");
scanf("%f",&k);
printf("res %f \n",Fn(k));
return 0;
}
There were several issues in your code:
Integer division: 1/n = 0
There was a confusion between the term value Fn and the sum value
An iterative solution is simpler here than a recursive one
Here is a code, with both iterative and recursive implementations:
#include <stdio.h>
#include <stdlib.h>
float sum_fn(int n){
float Fk = 1;
float sum = Fk;
for (int i = 1; i <= n; i++){
Fk = i*Fk + 1.0/i;
sum += Fk;
}
return sum;
}
float sum_recursive(int n, float *sum){
if (n == 0) {
*sum += 1.0;
return 1.0;
}
float Fn = n * sum_recursive(n-1, sum) + 1.0/n;
*sum += Fn;
return Fn;
}
int main(int argc, char *argv[]) {
int k;
printf("input k : ");
scanf("%d", &k);
printf("k = %d\tsum = %f\n", k, sum_fn(k));
float sum = 0;
sum_recursive(k, &sum);
printf("k = %d\tsum = %f\n", k, sum);
return 0;
}
To summarize my problem,
i want an array to return two array pointers. One of these pointer arrays must be float or double, this will give me the quotient operation. Another should be int, and that should give the remainder of the division.
For examle if i have two arrays as: int a[] = {3,6,9,12,16,18}, b[] = {2,3,3,4,4,4}; when i want to reach my pointers result should be like: Quotient is: {1.5,2,3,3,4,4.5} Remainder is: {1,0,0,0,0,2}
Here is my codes:
#include<stdio.h>
void div(int a[], int b[], float *quotient, int *remainder) {
float quo[6];
int remain[6];
for(int i = 0; i< 6 ; i++)
{
quo[i] = a[i] / (double)b[i];
remain[i] = a[i] % b[i];
*remainder = remain[i];
*quotient = quo[i];
*remainder++;
*quotient++;
}
// quotient = quo;
// remainder = remain;
}
int main() {
int a[] = {3,6,9,12,16,18}, b[] = {2,3,3,4,4,4};
float q;
int r;
div(a, b, &q, &r);
for(int i = 0; i< 6 ; i++)
{
printf("Quotient is: %.1f\nRemainder is: %d\n", q, r);
}
// printf("Quotient is: %.1f\nRemainder is: %d\n", *q, *r);
return 0;
}
You need to pass an array for the quotient and remainder into the divide function.
Than you can read the values after the function returns.
#include<stdio.h>
void div(int* a, int* b, float* quotient, int* remainder, int count) {
for (int i = 0; i < count; i++)
{
quotient[i] = a[i] / (double)b[i];
remainder[i] = a[i] % b[i];
}
}
int main() {
int a[] = { 3,6,9,12,16,18 }, b[] = { 2,3,3,4,4,4 };
#define LENGTH (sizeof(a) / sizeof(int))
float quotient[LENGTH];
int remainder[LENGTH];
div(a, b, quotient, remainder, LENGTH);
for (int i = 0; i < LENGTH; i++)
{
printf("Quotient is: %.1f\nRemainder is: %d\n", quotient[i], remainder[i]);
}
return 0;
}
long double power1(double x, int n) {
long double power = 1;
int i;
for(i=1; i<=n; i++) {
power *= i;
}
return power;
}
long long int factorial(long long n) {
long long int pr = 1;
long long int i;
for(i=1; i<=n; i++){
pr *= i;
}
return pr;
}
double myExp(double x, double epsi) {
double sum=(double)1;
int i;
while(power1(x, i)/((1.0)*factorial(i)) - epsi <= 0) {
sum += power1(x, i)/(1.0*factorial(i));
i++;
}
return sum;
}
int main(int argc, char *argv[]) {
system("cls");
double x, epsi;
int n;
x=1.5;
epsi=0.00001;
n=1000;
printf("exp(%.lf,%f)=%f\n", x, epsi, myExp(x, epsi));
printf("\n");
}
I need help to fix my exponential function e^x given the epsi value and x value such that abs(x^n/n!) <= epsi, where n is the first integer satisfying the condition. It seems the while loop doesn't work. When I enter 1.5, the desired output is 4.481689, but the result is 1.0000.
I am writing a program that creates an array of ten integers. I have to define max 10 constant and use functions.getdata(ask user for numbers),displaydata(display)
displaylargest,smallest,average,range,and median.
I am stuck on average because soon as I added that function my largest displays a weird number but if I comment out the average function my largest displays correct answer. Can someone tell me where I went wrong?
#include <stdio.h>
#define MAX 10
int getdata(int array[]);
int displaylargest(int array[]);
int displaysmallest(int array[]);
int displayaverage(int array[]);
void displaydata(int array[]);
int main () {
int array[MAX];
int largest;
int smallest;
int average;
printf("\nEnter ten numbers \n\n");
getdata(array);
displaydata(array );
largest=displaylargest( array);
printf("\nThe largest %d\n", largest);
smallest=displaysmallest( array);
printf("\nThe smallest is %d\n", smallest);
average=displayaverage(array);
printf("\nThe average is %d\n", average);
return 0;
}
int getdata(int array[]) {
int x;
printf ("Enter a number\n ",x+1);
for(x=0;x<MAX;x++)
scanf ("%d",&array[x]);
}
int displaylargest(int array[]) {
int x, largest=array[x];
for (x=0; x<MAX; x++) {
if (array[x]>largest)
largest=array[x];
}
return(largest);
}
int displaysmallest(int array[]) {
int x, smallest=array[x];
for (x=0; x<MAX; x++) {
if (array[x]<smallest)
smallest=array[x];
}
return(smallest);
}
int displayaverage(int array[]) {
int x;
int sum=0;
int average;
for (x=0; x<MAX; x++) {
sum+=array[x];
}
{
average=sum/MAX;
}
return(average);
}
void displaydata(int array[]) {
int x;
for(x=0; x<MAX; x++) {
printf("%d, ",array[x]);
}
}
You need to initialize local variables in your functions especially when you are using that to access your array. Without that local variable can contain any value ,if you use that as index to your array, you might be accessing valid memory.
#include <stdio.h>
#define MAX 10
void getdata(int array[]);
int displaylargest(int array[]);
int displaysmallest(int array[]);
int displayaverage(int array[]);
void displaydata(int array[]);
int main() {
int array[MAX];
int largest;
int smallest;
int average;
printf("\nEnter ten numbers \n\n");
getdata(array);
displaydata(array);
largest = displaylargest(array);
printf("\nThe largest %d\n", largest);
smallest = displaysmallest(array);
printf("\nThe smallest is %d\n", smallest);
average = displayaverage(array);
printf("\nThe average is %d\n", average);
return 0;
}
void getdata(int array[]) {
int x;
printf("Enter a number\n " );
for (x = 0; x<MAX; x++) {
scanf("%d", &array[x]);
}
}
int displaylargest(int array[]) {
int x, largest = array[0];
for (x = 0; x<MAX; x++) {
if (array[x]>largest)
largest = array[x];
}
return(largest);
}
int displaysmallest(int array[]) {
int x, smallest = array[0];
for (x = 0; x<MAX; x++) {
if (array[x]<smallest)
smallest = array[x];
}
return(smallest);
}
int displayaverage(int array[]) {
int x;
int sum = 0;
int average;
for (x = 0; x<MAX; x++) {
sum += array[x];
}
{
average = sum / MAX;
}
return(average);
}
void displaydata(int array[]) {
int x;
for (x = 0; x<MAX; x++) {
printf("%d, ", array[x]);
}
}
In c if we have local variable without initialization ,then it will have some garbage value. And in many of the functions u are intializing somevalue = array(x) it is not correct. The location you are accessing on the array is not valid.Make those chages it will work correctlu
Local variables need to be used here. While defining
largest=array[x];
make sure that x=0, else x may contain garbage value and the array may try to access invalid memory locations, leading to either the conditions known as underflow or overflow.
Also, I see that you've declared getdata() of type int, though it is not returning any value. Please make it of type void, as is just used to insert the asked values into the array. Hope this helps!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm having trouble using both the functions listed below in my c program main because of the implicit declaration error to the twos function. Could someone please advise.
Thanks!
#include <stdio.h>
#include <stdlib.h>
float twoxaverage(int n, float t__scores[]);
float getaverage(int n, float t_scores[])
{ int sum=0; float average=0; int i;
for (i=0; i<n; i++)
{
sum=sum + t_scores[i];
}
average=(float)sum/(float)n;
return(average);
}
float twoxaverage(int n, float t__scores[])
{ float mult;
mult=2*(getaverage( n, t__scores));
return (mult);
}
int main()
{
int t_score[]={1,2,3,4};
float twox;
twox=twoaverage4,t_score);
float twoxaverage(int n, float t__scores[]);
return 0;
}
Your main should move from:
int main(){
int t_score[] = { 1, 2, 3, 4 }; float twox;
twox = twoaverage4, t_score); float twoxaverage(int n, float t__scores[]);
return 0;
}
TO:
int main(){
int t_score[] = { 1, 2, 3, 4 };
float twox;
twox = twoxaverage(4, t_score);
return 0;
}
as a start.
I might also simplify your:
float getaverage(int n, float t_scores[]){
int sum=0; float average=0; int i;
for (i=0; i<n; i++){
sum=sum + t_scores[i];
}
average=(float)sum/(float)n;
return(average);
}
TO:
float getaverage(int n, float t_scores[]){
float average=0; int i;
for (i=0; i<n; i++){
average = average + t_scores[i];
}
return(average / (float)n);
}
This uses one less variable and performs the same math.
In this line you have at least two typos in one identifier
twox=twoaverage4,t_score);float twoxaverage(int n, float t__scores[]);
Instead of
twox=twoaverage4,t_score);
there shall be
twox = twoxaverage(4,t_score);
Also the declaration of the function that follows is unnecessary. So instead of
twox=twoaverage4,t_score);float twoxaverage(int n, float t__scores[]);
you can write simply
twox = twoxaverage(4,t_score);
I think you have not only to get the average but also to output it. So you could at least add statement
std::cout << twox << std::endl;
Or if it is a C program then you could use printf instead of C++ operator << to output twox
Also take into account that your function getaverage is wrong. You convert float numbers to int when add them to sum
float getaverage(int n, float t_scores[])
{ int sum=0; float average=0; int i;
for (i=0; i<n; i++)
{
sum=sum + t_scores[i];
}
average=(float)sum/(float)n;
return(average);
}
The valid function could look the following way
float getaverage( int n, const float t_scores[] )
{
float sum = 0.0f;
for ( int i = 0; i < n; i++ )
{
sum += t_scores[i];
}
return ( n == 0 ? 0.0f : sum / n );
}
Or if you use an old C compiler then the function can be written as
float getaverage( int n, const float t_scores[] )
{
float sum = 0.0f;
int i = 0;
for ( ; i < n; i++ )
{
sum += t_scores[i];
}
return ( n == 0 ? 0.0f : sum / n );
}
Function twoxaverage could be simplified the following way
inline float twoxaverage( int n, const float t__scores[] )
{
return ( 2 * getaverage( n, t__scores ) );
}