This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have to write a program in c language and this is the code : The problem is that when I try to compile it it says : syntax error before return .Where is my error?
#include <stdio.h>
int main (void)
{
char i,c2,j;
int c=4;
i=j=3;
while (++i <=c)
{
int j=1;
printf("\n Nr1=%c Nr2=%d",64+i,c2);
} do;
return 1;
}
Remove the do from your code. Just while (++i <= c) { /* ... */ };
You are redeclaring j inside the while loop. Remove int j=1;
and remove the do; at the end of your while
There is no such thing as while ... do loop in C. There are while loops or do ... while loops.
Related
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am trying to randomize a variable and then trying to use it in the functions where I want to use. But, when I put the randomized variable in for loop like below and when I use that in the function that I want to then it gives me error.
#include <stdlib.h>
#include "time.h"
void main (void) {
for (int i = 0; i < 4; i++) {
srand( time(NULL ));
float r;
r = rand()*1000;
}
write(abc, r);
read(abc, r);
write(xyz, r);
read(xyz, r);
}
So, when I use r from the for loop I get the error below:
In function 'void sim()':
'r' was not declared in this scope
But, when I remove r from the for loop then there is not any error. But, I want for loop so as to have different data for every write function.
Any suggestions would be appreciated.
r is a local variable for the loop and it is not visible outside of the loop. To fix this place its declaration(float r;) before the loop.
The scope of r is only inside the for loop. Declare it prior to the loop, and you'll be good to go.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
i am trying to compare array elements and idea is to collect all dissimilar element to other temp[] array. I think it is going somewhere wrong ...unable to understand...please help me
#include <stdio.h>
#include <stdlib.h>
#define SIZE 30
int my_arr[SIZE] = {10,20,45,63,89,20,15,12,89,24,12,10,89,25,64,39,37,64,95,
27,23,58,97,23,18,56,94,76,32,11
};
int main()
{
int i,j,temp_arr[100];
for(i=0;i<SIZE;i++)
{
for(j=0+i; j<SIZE; j++)
{
if(*(my_arr+i)!=*(my_arr+j))
*(my_arr+i) = temp_arr[i];
}
}
return 0;
}
Here:
*(my_arr+i) = temp_arr[i];
temp_arr[j] is not initialized and you are assigning it to my_arr[i]. You description sounds like you want:
temp_arr[i] = *(my_arr+i);
But then you will end up having holes in the temp_arr. So perhaps you need another index for counting items in the temp_arr.
Something like:
int tmp_cnt = 0;
for(i=0;i<SIZE;i++) {
for(j=0+i; j<SIZE; j++) {
if(*(my_arr+i)!=*(my_arr+j))
temp_arr[tmp_cnt++] = *(my_arr+i);
}
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have been doing A LOT of exercises and examples to get the hang of C, its finally starting to sink in slowly, but surely. Though I'm having one small problem with this and can't amend it for the life of me:
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, _TCHAR * argv[]) {
int i, grades[] = {98, 87, 92, 79, 85};
for(i = 0; i <= 4; ++i) {
printf("Element %d is %d\n"
i, grades[i]);
}
i = getchar();
return 0;
}
The problem is the i it is expecting a bracket, I don't know why and any amendment I have tried has failed, I know its a simple problem but it has me stuck.
Any help would be greatly appreciated.
The problem is that you're missing a comma that separates arguments in your call to printf:
printf("Element %d is %d\n", i, grades[i]);
// ^
// This one
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have written a code to find nth ugly number(a number which has at least one prime factor grater than 5) where n is a given input. my program runs well if the user inputs something less than 240. But if input gets bigger than that, program crashes!. My question is if it is a time consuming problem then it should take time but why the program crashes? I have used double everywhere so it might not be the matter of variable capacity!!
my code is below:
#include<stdio.h>
#include<math.h>
double primes[1000]={2,3,5};
int serial=3;
double next_prime()
{
double f=primes[serial-1]+2;
int count;
for(count=1;primes[count]<=(sqrt(f)+1) && count<serial;count++){
if(fmod(f,primes[count])==0){
f+=2;
count=1;
}
}
return primes[serial++]=f;
}
int main()
{
double ugly_serial=12,ugly_number=16,j;
int c,count,loop,input;
scanf("%d",&input);
while(ugly_serial<input)
{
loop=0;
for(c=3;primes[c-1]<=sqrt(ugly_number);c++){
j=next_prime();
}
for(count=3;count<c;count++){
if(fmod(ugly_number,primes[count])==0){
loop=1;
break;
}
}
if(loop==0){ugly_serial++;}
ugly_number++;
}
printf("%.0lf",ugly_number);
return 0;
}
I have compiled and run your code. The program works fine with all the input I have tried, including 56565.
Are you sure you are running the most recently compiled version of your program?
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Ok this is a simple C code but for some unknown reason the program refuse to compile and give segmentation fault 11 Please help me
#include <stdio.h>
typedef struct {
int P_answer[9];
int number;
} BOX;
int main()
{
BOX Matrix[8][8];
int i,j;
int k;
for(i=0;i<9;i++){
for(j=0;j<9;j++){
Matrix[i][j].number=0;
Matrix[i][j].P_answer[0]=1;
Matrix[i][j].P_answer[1]=2;
Matrix[i][j].P_answer[2]=3;
Matrix[i][j].P_answer[3]=4;
Matrix[i][j].P_answer[4]=5;
Matrix[i][j].P_answer[5]=6;
Matrix[i][j].P_answer[6]=7;
Matrix[i][j].P_answer[7]=8;
Matrix[i][j].P_answer[8]=9;
}
}
}
Matrix is an 8-by-8 array; each of your loops goes through 9 iterations.
The indexes of an array go from 0 to (size-1).
In your for-loops you go from 0 to size.
That's the reason of your segmentation fault.