Bubble Sorting Routine - c

Hi I am newer to c language and When I compile the current code to find the variance I get the following error: expected expression before']' token var=var+pow((x[]-my_mean(n,double x[])),2);
#include
#include
double my_var(int n, double x[]);
double my_mean(int n, double x[]);
double my_sum(int n, double x[]);
int main (void)
{
int n=5;
double x[]={4, 6, 2, 7, 9};
my_var(n,x);
return 0;
}
double my_mean( int n , double x[])
{
return my_sum(n, x)/ n;
}
double my_sum(int n , double x[])
{
int i;
double s=0;
for( i= 0; i<n ;i++)
{
s = s + x[i];
}
return s;
}
double my_var(int n, double x[])
{
double var;
int i;
for (i=0;i<n;i++)
{
var=var+pow((x[]-my_mean(int n,double x[])),2);
}
return var;
}

Okk.. I have got something figured out with you code.. Have a look::
#include <stdio.h>
#include <math.h>
double my_var(int n, double x[]);
double my_mean(int n, double x[]);
double my_sum(int n, double x[]);
int main (void)
{
int n=5;
double x[]={4, 6, 2, 7, 9};
printf("%f",my_var(n,x));
return 0;
}
double my_mean( int n , double x[])
{
return my_sum(n, x)/ n;
}
double my_sum(int n , double x[])
{
int i;
double s=0;
for( i= 0; i<n ;i++)
{
s = s + x[i];
}
return s;
}
double my_var(int n, double x[])
{
double var=0;
int i;
for (i=0;i<n;i++)
{
var=var+pow((x[i]-my_mean(n,x)),2);
}
return var;
}
Just added a print statement at the end of the main to check the result. In the for loop of your code in the function double my_var(int n, double x[]) you have a statement ::var=var+pow((x[]-my_mean(int n,double x[])),2);, firstly you cannot specify the data types in the function calls. Further, in the pow function you write (x[]-my_mean(int n,double x[])),2, instead of x[] it should be x[i], you need to specify an index.
Hope this helps.

and when you include math.h in your program then you have to compile it by the command "gcc my_program.c -o my_program -lm"

You forgot to use the loop index. Also, remove types from your call.
var=var+pow((x[i]-my_mean(n,x[i])),2);

Related

Function average and stdDev const int tab[ ]. Average problems

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;

Changing **array + a to *array[a]

What my program does is it finds 2 numbers of an array that are closest to the average, one is bigger, one is smaller. It works fine, however I need to change for example **array+a to *array[a].
However, when I load the program, it crashes after I input the numbers. If I try to print *array[0], *array[1], etc. it works fine. When I try to print or just do something with *array[a], *array[b], it crashes. Thank you for your help.
#include <stdio.h>
#include <stdlib.h>
int input (int *t, int *array[]);
void calculation (int *array[], int *t, int *x, int *y);
void output (int *x, int *y);
int main()
{
int *array, t, x, y;
input (&t, &array);
calculation (&array, &t, &x, &y);
output (&x, &y);
return 0;
}
int input (int *t, int *array[])
{ int n, *ptr;
printf ("How big is the array?");
scanf ("%d", &n);
ptr = (int*) malloc(n * sizeof(int));
int k;
printf ("Enter the numbers:");
for (k=0; k<n; k++)
{ scanf ("%d", ptr + k);
}
*t=n;
*array=ptr;
return 0;
}
void calculation (int *array[], int *t, int *x, int *y)
{ float sum=0, avg;
int min, max;
int more, less;
int a, b, c;
for (a=0; a<(*t); a++)
{sum=sum+ **array + a;
}
avg=sum/(*t);
min= *array[0];
max= *array[0];
for (b=0; b<(*t); b++)
{ if (max < (**array + b)) max=(**array + b);
if (min > (**array + b)) min=(**array + b);
}
more=max;
less=min;
for (c=0; c<(*t); c++)
{ if (((**array + c) < avg) && ((**array + c) > less)) less=(**array + c);
if (((**array + c) > avg) && ((**array + c) < more)) more=(**array + c);
}
*x=less;
*y=more;
}
void output (int *x, int *y)
{ printf("Number that is less than the average:%d\n", *x);
printf("Number that is more than the average:%d\n", *y);
}
It would be better to rethink your function prototypes a bit. It makes sense to pass a pointer to array to the input() function since you are allocating memory for it, and you want to be able to access it when you return. But you don't need to pass in the pointer to int t; instead, just return the value of n, and assign it to t in main.
There is no reason to pass a pointer to array to the function calculation(), since you are not changing the array allocation. You can also pass in the value of t from main(), since you only use this value in calculation(), but do not change it.
Similarly, the output() function only needs copies of x and y, since it does not change them.
The rule of thumb here is that you pass a pointer to a value into a function when you want to modify the value inside the function and have access to the modified value in the calling function. But you can also return a value instead of using a pointer to it.
These changes do not alter the functionality of your code, but they substantially improve its readability. You even get a sense of what is being modified in each function just by looking at the function prototypes. Well, the changes do alter the functionality in that your original **array + a was incorrect, and needed to be either *(*array + a) or (*array)[a]. But sorting that problem out should help you to appreciate the virtue of the simpler function prototypes. Here is the modified code:
#include <stdio.h>
#include <stdlib.h>
int input(int *array[]);
void calculation(int array[], int t, int *x, int *y);
void output(int x, int y);
int main(void)
{
int *array, t, x, y;
t = input(&array);
calculation(array, t, &x, &y);
output(x, y);
return 0;
}
int input(int *array[])
{ int n, *ptr;
printf("How big is the array?");
scanf("%d", &n);
ptr = (int*) malloc(n * sizeof(int));
int k;
printf("Enter the numbers:");
for (k=0; k<n; k++)
{ scanf("%d", ptr + k);
}
*array=ptr;
return n;
}
void calculation(int array[], int t, int *x, int *y)
{ float sum=0, avg;
int min, max;
int more, less;
int a, b, c;
for (a=0; a<t; a++)
{sum=sum+ array[a];
}
avg=sum/t;
min= array[0];
max= array[0];
for (b=0; b<t; b++)
{ if (max < array[b]) max=array[b];
if (min > array[b]) min=array[b];
}
more=max;
less=min;
for (c=0; c<t; c++)
{ if ((array[c] < avg) && (array[c] > less)) less=array[c];
if ((array[c] > avg) && (array[c] < more)) more=array[c];
}
*x=less;
*y=more;
}
void output(int x, int y)
{ printf("Number that is less than the average:%d\n", x);
printf("Number that is more than the average:%d\n", y);
}
Just like BLUEPIXY and Some programmer dude said, it's supposed to be (*array)[a]

why am I getting result=0.00000

I'm trying to calculate sin and cos without using math library with taylor series witch is
sinx =∑n=0 to n=∞ (-1)^n * x^(2n+1) / (2n+1)! and this code produces whatever the input is 0,00000 for sin 1,00000 for cos where is the problem in this code
#include <stdio.h>
#include <stdlib.h>
double fakt(int);
double power(int,int);
int negative_positive(int);
double calculate_sin(int,int);
double calculate_cos(int,int);
double calculate_radyan(int);
int main() {
int degree,number;
char command;
do{
scanf("%c",&command);
if(command=='d' || command=='D'){
scanf("%d %d",&degree,&number);
double radyan = calculate_radyan(degree);
printf("%lf \n%lf ",calculate_sin(radyan,number),calculate_cos(radyan,number));
}
}while(command!='e' && command!='E');
return 0;
}
double fakt(int n){
int i;
double result=1;
for(i=1;i<n;i++)
result = result*i;
return result;
}
double power(int base, int exponent){
int i;
double result=1;
for(i=0;i<exponent;i++)
result = result*base;
return result;
}
int negative_positive(int number){
if(number % 2 == 0)
return 1;
else
return -1;
}
double calculate_sin(int degree , int n){
int i;
double result=0;
double tmp=0;
for(i=0;i<n;i++){
tmp=negative_positive(i)*power(degree,2*i+1)/fakt(2*i+1);
result=tmp+result;}
return result;
}
double calculate_cos(int degree , int n){
int i;
double result=0;
double tmp=0;
for(i=0;i<n;i++)
{
tmp=negative_positive(i)*power(degree,i*2)/fakt(2*i);
result=tmp+result;
}
return result;
}
double calculate_radyan(int degree){
double result,pi=3.14159;
result =pi/180*degree;
return result;
}
ı will answer my own question but ı found the solution. when program trys to go to power method with double radyan variable it converts that variable to 0 cause that method just takes integer variables power(double base,int exponent) is solved my problem

Implicit declaration of function even after header inclusion [closed]

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 ) );
}

Passing multidimensional arrays to a function

The point of the program is to send data from 1 array to another array I'm not sure what's wrong with how I'm passing it. It should enter the data in 1 array then call upon the copy function and puts itself there and then the array is traversed.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
#include<math.h>
#include<ctype.h>
#include<stdbool.h>
double copy_arr(double source[n][u],double target[n][u],int n,int u);
int main(void)
{
double source[3][5]={{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5}};
double target1[3][5];
copy_arr(source,target1,3,5);
int j;
int i;
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
printf("%f 1",target1[i][j]);
}
}
return 0;
}
double copy_arr(double source[][],double target[][],int n,int u)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<u;j++)
{
target[i][j] = source[i][j];
}
}
return target[n][u];
}
Your function prototype is wrong because compiler has not seen n and u yet. Your program does not even compile.
Change
double copy_arr(double source[n][u],double target[n][u],int n,int u);
to
double copy_arr(int n,int u,double source[n][u],double target[n][u]);
Or you could do hardcoded array size
#define SIZE_ARR 5
void copy_arr(double source[][SIZE_ARR], double target[][SIZE_ARR], int n, int u);
int main(void)
{
double source[3][SIZE_ARR]={{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5}};
double target1[3][SIZE_ARR];
copy_arr(source,target1, 3, 5);
int j;
int i;
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
printf("%f 1",target1[i][j]);
}
}
return 0;
}
void copy_arr(double source[][SIZE_ARR], double target[][SIZE_ARR], int n, int u)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<u;j++)
{
target[i][j] = source[i][j];
}
}
}

Resources