Ok so I get this code to do the averaging : (written in C )
.
.
int sum[3];
int j;
int avg;
for(;;) //infinite loop
{
for(j=0;j<3;j++){
i = ReadSensor(); // function that keeps saving sensor values as int i
sum[j]=i;
}
avg=sum[0]+sum[1]+sum[2]+sum[3];
printf("Sonar: %d \r \n", avg >> 2);
}
.
.
Is this correct ? im shifting by 2 to divide by avg / 2^(2) which is 4
The problem is im expecting a value of about 15, however I get about 8--9 .. Im not sure why this is happening ?
Basically the sensor's readings fluctuate between 15-17, I want to get an average instead of printing noise values. Is my code correct ? Then why do I get wrong outputs !?
Looks like your script only captures three values (j=0, j=1, j=2), then divides by four.
You have a few problems, here are some suggestions:
You're iterating through the inside loop 3 times, however you're saying you have 4 sensors, you should change your for loop to: for (j = 0; j < 4; j++).
sum is an array of 3 elements, yet you're accessing an element 1 past the end of the array when calculating avg (sum[3]). This will cause undefined behaviour. sum should be declared as char sum[4] for this reason and the one above.
(Optional) sum does not need to be an array in the above example, it can simply be an int.
(Optional) If you want to divide an int by 4, use the division operator. The compiler should be better at optimizing the code for your particular architecture than you.
This is how your code could now look, depending on whether you need to keep an array or not:
int sum[4];
int total, j;
for (;;)
{
total = 0; /* reset at every iteration of the outside loop */
for (j = 0; j < 4; j++) {
sum[i] = ReadSensor();
total += sum[i];
}
printf("Sonar: %d \r \n", total / 4);
}
OR
int total, j;
for (;;)
{
total = 0; /* reset at every iteration of the outside loop */
for (j = 0; j < 4; j++)
total += ReadSensor();
printf("Sonar: %d \r \n", total / 4);
}
Isn't this
avg=sum[0]+sum[1]+sum[2]+sum[3];
should be
avg=sum[0]+sum[1]+sum[2];
as the loop as well declaration int sum[3]; means we are trying to store only 3 values.
Now if you want 4 and ok with divide operator. There are the new code which should replace the mentioned lines
int sum[4];
for(j=0;j<4;j++)
avg=sum[0]+sum[1]+sum[2]+sum[3]; // this part stays the same
The number of values read from sensor is required twice. First, to control the number of iterations of for loop. Second, as the divisor of sum. Introduce a variable (say, N) to capture that.
Also, the division by shifting does not sound right, because that restricts the number of readings from the sensor to power of two.
enum { N = 4 };
sum = 0;
for( j = 0; j < N; j++) {
i = ReadSensor(); // function that keeps saving sensor values as int i
sum += i;
}
avg = sum / N;
printf( "Sonar average: %d\n", avg );
Related
Program not working, not giving output, I don't know what to do, where the problem is.
I'm trying to find out the largest palindrome made from the product of two 3-digit numbers.
#include <stdio.h>
main() {
int i, k, j, x;
long int a[1000000], palindrome[1000000], great, sum = 0;
// for multiples of two 3 digit numbers
for (k = 0, i = 100; i < 1000; i++) {
for (j = 100; j < 1000; j++) {
a[k] = i * j; // multiples output
k++;
}
}
for (i = 0, x = 0; i < 1000000; i++) {
// for reverse considered as sum
for (; a[i] != 0;) {
sum = sum * 10 + a[i] % 10;
}
// for numbers which are palindromes
if (sum == a[i]) {
palindrome[x] = a[i];
x++;
break;
}
}
// comparison of palindrome number for which one is greatest
great = palindrome[0];
for (k = 0; k < 1000000; k++) {
if (great < palindrome[k]) {
great = palindrome[k];
}
}
printf("\ngreatest palindrome of 3 digit multiple is : ", great);
}
What do you mean with "not working"?
There are two things, from my point of view:
1) long int a[1000000], palindrome[1000000]
Depending on you compile configuration you could have problems compiling your code.
Probably the array is too big to fit in your program's stack address space.
In C or C++ local objects are usually allocated on the stack. Don't allocate it local on stack, use some other place instead. This can be achieved by either making the object global or allocating it on the global heap.
#include <stdio.h>
long int a[1000000], palindrome[1000000], great, sum = 0;
main() {
int i, k, j, x;
2) printf("\ngreatest palindrome of 3 digit multiple is : ", great);
I will change it by :
printf("\ngreatest palindrome of 3 digit multiple is %li: ", great);
Regards.
Compiling and running your code on an on-line compiler I got this:
prog.c:3:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main() {
^
prog.c:34:61: warning: data argument not used by format string [-Wformat-extra-args]
printf("\ngreatest palindrome of 3 digit multiple is : ", great);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
2 warnings generated.
Killed
Both the warnings should be taken into account, but I'd like to point out the last line. The program was taking too much time to run, so the process was killed.
It's a strong suggestion to change the algorithm, or at least to fix the part that checks if a number is a palindrome:
for (; a[i] != 0;) { // <-- If a[i] is not 0, this will never end
sum = sum * 10 + a[i] % 10;
}
I'd use a function like this one
bool is_palindrome(long x)
{
long rev = 0;
for (long i = x; i; i /= 10)
{
rev *= 10;
rev += i % 10;
}
return x == rev;
}
Also, we don't need any array, we could just calculate all the possible products between two 3-digits number using two nested for loops and check if those are palindromes.
Starting from the highest numbers, we can store the product, but only if it's a palindrome and is bigger than any previously one found, and stop the iteration of the inner loop as soon as the candidate become less then the stored maximum. This would save us a lot of iterations.
Implementing this algorithm, I found out a maximum value of 906609.
I have this simple program I am working on in class, it initialized a 3x5 2d Array of integers whose values are inputted for each cell by the user. The program then calls a function which runs through the array with a for loop to display each value, then calls a function which again uses a for loop to double every value, and calls the previous display function to show the array again.
All of this seems to be working, however I am consistently getting odd outputs for certain areas when initializing the values for the 2dArray.
For example: Entering 5 rows of "1, 2, 3" and then calling the display function produces this as output:
1,1,2,
1,2,3,
1,2,3,
1,2,5,
1,2,3
Further more, the doubling function produces further strange results but only in the areas where the output was different from what the user had inputted.
Output of the double function on the array I just posted displays as:
2,4,8
2,4,6
2,4,6
2,4,10,
4,8,6
The only real mathematical operation in the entire program is in the doubling functions, where it runs through a for loop setting the value of "array[j][i] = (array[j][i] = array[j][i] * 2)"
I cannot for the life of me figure out which part of the program I've written would cause the user inputs to change to what has been displayed. Inputting values other than "1,2,3" produces similarly odd results. Anyone have any idea what is wrong here? I feel like it must be a very simple mistake I am missing. Here is my source code:
#include <stdio.h>
#include <stdlib.h>
void displayArray(int array[][4]);
void doubleArray(int array[][4]);
int main() {
int dArray[2][4];
int i, j, k;
for(i = 0; i <= 4; i++){
for(j = 0; j <= 2; j++){
printf("Enter a value for the array at position %d,%d.\n", j, i);
scanf("%d", &dArray[j][i]);
}
}
printf("Displaying your original array...\n");
displayArray(dArray);
printf("Doubling your array...\n");
doubleArray(dArray);
printf("Displaying your doubled array....\n");
displayArray(dArray);
system("pause");
}
void displayArray(int array[][4]){
int i, j;
for(i = 0; i <= 4; i++){
printf("\n");
for(j = 0; j <= 2; j++){
if(j == 2 && i == 4){
printf("%d", array[j][i]);
}
else{
printf("%d,", array[j][i]);
}
//system("pause");
}
}
printf("\n");
}
void doubleArray(int array [][4]){
int i, j;
for(i = 0; i <= 4; i++){
for(j = 0; j <= 2; j++){
array[j][i] = (array[j][i] * 2);
//printf("%d\n", array[j][i]);
}
}
}
It's all in one .c file, and I am using devc++ if that makes any difference.
To calculate the DIMENSIONS of your 2D C arrays you have to count how many columns and how many rows there are. In your examples, it is 3 columns and 5 rows, and those are the values you must enter in your array definition:
int array[3][5]
Then, because C starts indexing with 0 offset, you can referr to the elements of the array using the columns 0 to 2 (that is, 3 columns) and rows 0 to 4 (that is, 5 rows). As other people said, this can be achieved using "lower than the limit" (correct: <3 for the columns, <5 for the rows) instead of "lower or equal than the limit" (incorrect, out of bounds: <=3 for the columns, <=5 for the rows).
I'm trying to
Get rid of the info in an array with 10 "spots".
Fill the array with (10) random numbers
My code till time
int main()
{
int numbers[10] = { 0 };
int randNumber = 0;
int i = 0;
for (int i = 0; i <= 10; i++)
{
srand(time(NULL));
randNumber = rand() % 10 + 1;
printf("Random number saved in the array: %d\n", randNumber);
i++;
}
getchar();
getchar();
return 0;
}
First of all, you need to move the srand(time(NULL)); out of the loop.
Otherwise, because, time() has a time granularity of 1 second, in a second, if called multiple times in the loop (within a second, probably), it will re-initialize the PNRG with the same seed and all the next call to rand() will give you the same random number.
Now, once you have the random numbers, you need to assign it to the each array member like numbers[i] = randNumber; inside the loop, but there's more to it. Your loop, at present is off by one. You need to change
for (int i = 0; i <= 10; i++)
to
for (int i = 0; i < 10; i++)
to stay within bounds.
Your array's size is 10, and this loop runs 11 times, causing an overflow. This will solve it:
for (int i = 0; i < 10; i++)
Also remove the increasing of the loop's iterator, i, from inside the loop body. Remove the line:
i++;
i am trying to simulate the Ising Model 1-D. This model consists in a chain of spin (100 spins) and using the Mont Carlo - Metropolis to accept the flip of a spin if the energy of the system (unitary) goes down or if it will be less than a random number.
In the correct program, both the energy the magnetization go to zero, and we have the results as a Gaussian (graphics of Energyor the magnetization by the number of Monte Carlo steps).
I have done some work but i think my random generator isn't correctt for this, and i don't know how/where to implement the boundary conditions: the last spin of the chain is the first one.
I need help to finish it. Any help will be welcome. Thank you.
I am pasting my C program down:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h> //necessary for function time()
#define LENGTH 100 //size of the chain of spins
#define TEMP 2 // Temperature in units of J
#define WARM 200 // Termalização
#define MCS 20000 //Monte Carlo Steps
void start( int spin[])
{
/* starts with all the spins 1 */
int i;
for (i = 0 ; i < 100; i++)
{
spin[i] = 1;
}
}
double energy( int spin[]) //of the change function J=1
{
int i;
double energyX=0;// because the begining Energy = -J*sum (until 100) =-100,
for (i = 0;i<100;i++)
energyX=energyX-spin[i]*spin[i+1];
return(energyX);
}
int randnum(){
int num;
srand(time(NULL));
/* srand(time(NULL)) objectives to initiate the random number generator
with the value of the function time(NULL). This is calculated as being the
total of seconds passed since january first of 1970 until the present date.
So, this way, for each execution the value of the "seed" will be different.
*/
srand(time(NULL));
//picking one spin randomly zero to 100
num=rand() % 100;
printf("num = %d ", num);
return num;
}
void montcarlo( int spin[])
{
int i,j,num;
double prob;
double energyA, energyB; // A -> old energy and B -> the new energy
int rnum1,rnum2;
prob=exp(-(energyB-energyA)/TEMP);
energyA = 0;
energyB = 0;
for (i = 0;i<100;i++)
{
for (j = 0;j<100;j++)
{
energyA=energy(spin);
rnum1=randnum();
rnum2=randnum(); // i think they will give me different numbers
spin[rnum1] = -spin[rnum1]; //flip of the randomly selected spin
energyB = energyB-spin[j]*spin[j+1];
if ((energyB-energyA<0)||((energyB-energyA>0)&&(rnum2>prob))){ // using rnum2 not to be correlated if i used rnum1
spin[rnum1]=spin[rnum1];} // keep the flip
else if((energyB-energyA>0)&&(rnum2<prob))
spin[rnum1]=-spin[rnum1]; // unflip
}
}
}
int Mag_Moment( int spin[] ) // isso é momento magnetico
{
int i;
int mag;
for (i = 0 ; i < 100; i++)
{
mag = mag + spin[i];
}
return(mag);
}
int main()
{
// starting the spin's chain
int spin[100];//the vector goes til LENGHT=100
int i,num,j;
int itime;
double mag_moment;
start(spin);
double energy_chain=0;
energy_chain=energy(spin); // that will give me -100 in the begining
printf("energy_chain starts with %f", energy_chain);// initially it gives -100
/*Warming it makes the spins not so ordered*/
for (i = 1 ; i <= WARM; i++)
{
itime = i;
montcarlo(spin);
}
printf("Configurtion after warming %d \n", itime);
for (j = 0 ; j < LENGTH; j++)
{
printf("%d",spin[j]);
}
printf("\n");
energy_chain=energy(spin); // new energy after the warming
/*openning a file to save the values of energy and magnet moment of the chain*/
FILE *fp; // declaring the file for the energy
FILE *fp2;// declaring the file for the mag moment
fp=fopen("energy_chain.txt","w");
fp2=fopen("mag_moment.txt","w");
int pures;// net value of i
int a;
/* using Monte Carlo metropolis for the whole chain */
for (i = (WARM + 1) ; i <= MCS; i++)
{
itime=i;//saving the i step for the final printf.
pures = i-(WARM+1);
montcarlo(spin);
energy_chain = energy_chain + energy(spin);// the spin chain is moodified by void montcarlo
mag_moment = mag_moment + Mag_Moment(spin);
a=pures%10000;// here i select a value to save in a txt file for 10000 steps to produce graphs
if (a==0){
fprintf(fp,"%.12f\n",energy_chain); // %.12f just to give a great precision
fprintf(fp2,"%.12f\n",mag_moment);
}
}
fclose(fp); // closing the files
fclose(fp2);
/* Finishing -- Printing */
printf("energy_chain = %.12f\n", energy_chain);
printf("mag_moment = %.12f \n", mag_moment);
printf("Temperature = %d,\n Size of the system = 100 \n", TEMP);
printf("Warm steps = %d, Montcarlo steps = %d \n", WARM , MCS);
printf("Configuration in time %d \n", itime);
for (j = 0 ; j < 100; j++)
{
printf("%d",spin[j]);
}
printf("\n");
return 0;
}
you should call srand(time(NULL)); only once in your program. Every time you call this in the same second you will get the same sequence of random numbers. So it is very likely that both calls to randnum will give you the same number.
Just add srand(time(NULL)); at the begin of main and remove it elsewhere.
I see a number of bugs in this code, I think. The first one is the re-seeding of the srand() each loop which has already been addressed. Many of the loops go beyond the array bounds, such as:
for (ii = 0;ii<100;ii++)
{
energyX = energyX - spin[ii]*spin[ii+1];
}
This will give you spin[99]*spin[100] for the last loop, for which is out of bounds. That is kind of peppered throughout the code. Also, I noticed the probability rnum2 is an int but compared as if it's supposed to be a double. I think dividing the rnum2 by 100 will give a reasonable probability.
rnum2 = (randnum()/100.0); // i think they will give me different numbers
The initial probability used to calculate the spin is, prob=exp(-(energyB-energyA)/TEMP); but both energy values are not initialized, maybe this is intentional, but I think it would be better to just use rand(). The Mag_Moment() function never initializes the return value, so you wind up with a return value that is garbage. Can you point me to the algorithm you are trying to reproduce? I'm just curious.
Hello Stackoverflow crew. I'm a very amateur C programmer and I'm working on a program that reads some input about wedding gifts, and then outputs information that includes the maximum gift value, the minimum gift value, the total average of the gift values, and the average of the gifts that were valued at x > 0. I've finished writing everything, but the program always seems to crash after the first loop. I've been looking at it for the past few hours, so I'm having issues finding what the error might be. Here is the code I have:
#include <stdio.h>
#include <stdlib.h>
int main() {
//Opens the file and creats a pointer for it.
FILE *ifp;
ifp = fopen("gifts.txt", "r");
//Declares the variables
int i, j, k, l, m, n, o, p, q, x, y;
int gift_sets, num_gifts, prices, max_value, max, avg_val, no_zero;
//Scans the file and assigns the first line to variable "gift_sets"
fscanf(ifp, "%d", &gift_sets);
//Begins a for loop that repeats based on the value of gift_sets
for (i = 0; i < gift_sets; i++) {
printf("Wedding Gifts #%d\n", i + 1);
printf("Gift Value\t Number of Gifts\n");
printf("----------\t ---------------\n");
//Scans the price values into the array prices[num_gifts]
fscanf(ifp, "%d", &num_gifts);
int prices[num_gifts];
//Creates a loop through the prices array
for (j = 0; j < num_gifts; j++){
fscanf(ifp, "%d", &prices[j]);
}
//Declares a frequency array
int freq[max + 1];
for (k = 0; k <= max; k++) {
freq[k] = 0;
}
for (l = 0; l < num_gifts; l++) {
freq[prices[l]]++;
}
for (m = 0; m < max + 1; m++) {
if (freq[m] > 0){
printf("%d\t%d",m, freq[m]);
}
}
printf("\n");
//Zeroes the variable "max_val."
int max_val = prices[0];
//Loops through the array to find the maximum gift value.
for (n = 0; n < num_gifts; n++){
if (prices[n] > max_value)
max_value = prices[n];
}
// Zeroes "min_val."
int min_val = prices[0];
//Finds the lowest value within the array.
for(o = 0; o < num_gifts; o++){
if(prices[o] !=0){
if(prices[o] < min_val){
min_val = prices[o];
}
}
}
//Calculates the total number of gifts.
double sum_gifts = 0;
for(p = 0; p < num_gifts; p++){
sum_gifts = sum_gifts + prices[p];
}
//Calculates the average value of all the gifts.
avg_val = (sum_gifts / num_gifts);
//find non zero average
double x = 0;
int y = 0;
for(q = 0; q < num_gifts; q++){
if (prices[q] != 0){
x += prices[q];
y++;
}
}
//Calculates the average value of the gifts, excluding the gifts valued zero.
int no_zero = x / y;
//Prints the maximum gift value.
printf("The maximum gift value is: $%d", max_value);
printf("\n");
//Prints the minimum gift value.
printf("The minimum gift value is: $%d\n", min_val);
//Prints the average of all the gifts.
printf("The average of all gifts was $%.2lf\n",avg_val);
//Prints the no zero average value of the gifts.
printf("The average of all non-zero gifts was $%.2lf",no_zero);
printf("\n\n\n");
}
return 0;
}
Thanks in advance for the help guys. As always, it's much appreciated.
EDIT: To further elaborate, the "crash" is a windows error "gifts.exe has stopped working" when executing the program. It does say at the bottom of the window that "Process returned -1073741819 <0xC0000005>"
When you declare the array with the num_gifts variable, it generates assembly instructions which allocate enough space on the stack to hold num_gifts integers. It does this at compile-time. Normally this wouldn't compile, but depending on the behavior of the ms c compiler, it could compile and assume whatever value is put in num_gifts by default (maybe 0, maybe something else) is the length. When you access it, it's possible that you're trying to access an array with zero elements, which could cause an access violation.
I'll tell you one thing you should do, straight away.
Check the return values from fscanf and its brethren. If, for some reason the scan fails, this will return less than you expect (it returns the number of items successfully scanned).
In that case, your data file is not what your code expects.
You should also be checking whether ifp is NULL - that could be the cause here since you blindly use it regardless.
One thing you'll find in IDEs is that you may not be in the directory you think you're in (specifically the one where gifts.txt is).
And, on top of that, max ill be set to an arbitrary value, so that int freq[max+1]; will give you an array of indeterminate size. If that size is less than the largest price, you'll be modifying memory beyond the end of the array with:
freq[prices[l]]++;
That's a definite no-no, "undefined behaviour" territory.
At least at first glance, it looks like you haven't initialized max before you (try to) use it to define the freq array.