|
Message mapping and COM support.
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) (October 2009 - September 2013) Polymorphism in C
|
|
|
|
|
|
|
<blockquote class="quote"><div class="op">Quote:</div>#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include "Utility.c"
#include<dirent.h>
float* random1(int SizeOfData);
int main(void)
{
int i,j;
int SizeOfData=63530;
float* squaring;
float total_sum,summation_noise;
float* random_seq=(float *)malloc(SizeOfData* sizeof(float ));
random_seq=random1(SizeOfData);
for(i=0;i<SizeOfData;i++)
{
printf("\t%f\n",random_seq[i]);
}
return 0;
}
</blockquote>
// utility.c
<blockquote class="quote"><div class="op">Quote:</div>float* random1(int SizeOfData)
{
srand(12345);
int i,j;
float sum=0.0;
float a=0.2;
float summation_noise=0.0;
float *y;
y=(float *)malloc(SizeOfData* sizeof(float ));
for(i=0;i<SizeOfData;i++)
{
float f= ((float)rand()/(float)(RAND_MAX))*a;
y[i]=f;
}
return y;
</blockquote>
while running expected output has to be random float number from 0-63530
but it is displaying only one number .
|
|
|
|
|
Quote: #include "Utility.c"
What's its purpose (usually you include just header files)?
Your program (without such line) runs producing the expected output.
Please note:
Quote: float* random_seq=(float *)malloc(SizeOfData* sizeof(float ));
is useless, since memory is allocated inside the random1 function.
- You never free the allocated memory (you have to ).
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
utility.c is the program file where the function is declared from there I was aiming to call to main.c that y I include it.
|
|
|
|
|
maibam debina wrote: utility.c is the program file where the function is declared What function? You know, functon declarations should be inside header files and you should include just header files (as rule of thumb for beginners, at least).
Quote: rom there I was aiming to call to main.c that y I include it You cannot call a file (main.c is a file).
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
In Utility.c its consist of already one created header file Utility.h which consist of struct for waveread do i need to add on this and if needed which way i have to?
#define _USE_MATH_DEFINES
#define PI M_PI /* pi to machine precision, defined in math.h */
#define TWOPI (2.0*PI)
#ifndef UTILITY_H_
#define UTILITY_H_
struct WavHeader {
char chunkid[4];
int chunksize;
char format[4];
char subchunk1id[4];
int subchunk1size;
short int audioformat;
short int numchannels;
int samplerate;
int byterate;
short int blockalign;
short int bitspersample;
char subchunk2id[4];
int subchunk2size;
};
float * wavRead(char*, int*);
#endif /* UTILITY_H_ */
|
|
|
|
|
You should include Utility.h if you need either WavHeader or wavRead (or both).
Utility.c should contain the wavRead function definition (its implementation).
You should NOT include Utility.c in main.c .
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
Is it always neccessary to create header file,if i want to call a function which is in another file?
|
|
|
|
|
It is the good practice (the bad practice is copying the function declarations in the source code that needs them).
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
It is almost always necessary, and there is no reason not to use header files. Therefore you should never include a .c file.
It all comes down to the question where the actual implementation of the function can be found within the final executable program. The compiler will read your .c files one by one and generate the machine code for all functions implemented therein. All include statements will be resolved before the compiler even sees those files, therefore including another .c file is equivalent to copying the entire code of that .c file! This may result in multiple copies of the same function, and, as a result, the linker may be unable to generate a program.
Technically you can avoid these issues without the help of header files: all you need are declarations of the data types and functions declared somewhere, that you need in your current .c file. The header files are used as a means to assemble this information in just one place, rather than copy the declarations to every .c file that needs them. This way you can ensure that all files are properly updated with the relevant information, whenever the data type or function declarations change!
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
hi,
Your code is working fine & generating random sequence of 65350 times.
|
|
|
|
|
ya it is actually i was planning to make entire program as function and create another main.c that display only the output of the function..
|
|
|
|
|
Hello
My name is Shuvo Biswas. I am new to programming . Please, help me to choose a IDE that is easy to use and best among other.
Thank you .
|
|
|
|
|
|
|
<blockquote class="quote"><div class="op">Quote:</div>
#include"Utility.h"
#include"Utility.c"
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/param.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define FALSE 0
#define TRUE !FALSE
float square(float b);
char pathname[MAXPATHLEN];
int main(int argc,int *argv[])
{
int count,i;
struct direct **files;
int file_select();
if (getwd(pathname) == NULL )
{
printf("Error getting path\n");
exit(0);
}
count = scandir(pathname, &files, file_select, alphasort);
if (count <= 0)
{
printf("No files in this directory\n");
exit(0);
}
printf("\n Number of files = %d\n",count);
for (i=1;i<count+1;++i)
{
printf("\n%s\n ",files[i-1]->d_name);
srand(12345);
int i,j;
float y,sum=0.0;
float a=0.2;
float summation=0.0;
int SizeOfData;
float *data;
data=wavRead(files[i-1]->d_name,&SizeOfData) ;
printf("\n\n========================================================\n");
printf("Generated random number\t || Squaring of random number:\n");
printf("=========================================================\n");
for(i=0;i<SizeOfData;i++)
{
float f= ((float)rand()/(float)(RAND_MAX))*a;
printf("%f\t",f);
y=square(f);
printf("\t\t%f\n",y);
sum +=y;
}
printf("\n===================================================\n");
printf("\n TOTAL SUM OF THE SQUARED NUMBER := %f\t\n",sum);
summation=sum/SizeOfData;
printf("\n ENERGY OF NOISE SIGNAL := \t%f/%d=%f\n\n",sum,SizeOfData,summation);
FILE *fout;
fout=fopen("NOISE SIGNAL","w");
j=0;
sum=0.0;
srand(12345);
fprintf(fout,"\n================================================================\n");
fprintf(fout,"\nGENERATED RANDOM NUMBER || SQUARING OF GENERATED RANDOM NUMBER\n");
fprintf(fout,"\n=================================================================\n");
while(j<SizeOfData)
{
float f= ((float)rand()/(float)(RAND_MAX))*a;
y=square(f);
fprintf(fout,"\t%f\t\t\t%f\n",f,y);
sum +=y;
j++;
}
fprintf(fout,"\n===============================================\n");
fprintf(fout,"\nTOTAL SUM OF THE SQUARED NUMBER:=\t%f",sum);
summation=sum/SizeOfData;
fprintf(fout,"\nENERGY OF NOISE SIGNAL e(n):=\t%f/%d = %f\n\n",sum,SizeOfData,summation);
fclose(fout);
}
float square(float b)
{
float z;
z=b*b;
return(z);
}
</blockquote>
|
|
|
|
|
You could just convert this code into a function which you can then add to a different project.
|
|
|
|
|
can u help me little deeper how to convert it .?
|
|
|
|
|
What's the problem? Just change the file name to something else, and change "main" to the name of your new function, with the appropriate parameters and return type.
|
|
|
|
|
which procedure i have to run and do i need to create header file for the function?
|
|
|
|
|
You don't run anything. Just create a new source file, define your function, by name, parameters and return type. You then use that from another file, either by defining it in the source, or by providing a header file with the definition.
Something like ...
# include double myfunction(int count, double* numbers)
{
double result = 0.0;
return result;
}
int main()
{
double myset[] = { 1.0, 2.3, 4.4 };
extern double myfunction(int count, double* numbers);
double answer = myfunction(3, myset);
}
|
|
|
|
|
thanks as before Richard but in which way i have to run in terminal i m using ubuntu..
|
|
|
|
|
Use the editor (vi, emacs etc) to create your files and run your compiler or make processor to build them. If you really don't understand these basics of your system, then I suggest you get hold of, and study, a book or the Linux man pages.
|
|
|
|