Array and loops [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have an array contain 100 elements. Can anyone help me to figure out how to write loops that perform this:
data[0] = 1/(2*3*4)
data[1] = 1/(4*5*6)
data[2] = 1/(6*7*8)
...
data[99] = 1/(200*201*202)
data[0]-data[1]+data[2]-data[3]+data[4]-data[5]+...+data[98]-data[99]
I just can't understand how to start. Any suggestions would be appreciated!

Try this
double c=0;
for (int i=0;i<100;i++)
{
c=i*2+2;
data[i]=1/(c*(c+1)*(c+2));
}
for (int i = 0; i < 100; i+=2)
{
op+= data[i] - data[i+1];
}

My suggestions how to start, if you really want them and want to manage this by your own:
Generalize your algorithm:
Find a function f(x) such that data[i] = f(i)
Just write the algorithm in your native language.
Then learn basic operators of C language, including loop construct.
Write your "native language algorithm" in C language.

Just in one loop:
int total = 0;
for(size_t i=0; i<100; ++i){
int temp = (i+1)*2;
data[i] = 1/(temp*(temp+1)*(temp+2));
total = total + (i%2==0?data[i]:-data[i]);
}

Related

Why do I get a segmentation fault with strcmp [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I believe it is because of the strcmp(). I tried it multiple ways so far and this is just the latest. My objective is to get the index of the array so I can go on to a switch statement to execute code. Any help would be appreciated, although I'm only expecting a "You can't do that."
Big picture is to incorporate this snip of code into a "utility" file that has multiple "functions" and would call it like util("Ping") to execute a Ping and so on...
int main(){
char *cmd = "Ping";
char *names[3]={"Ping","Stop","Go"};
int index = 3;
int i;
char *test;
for (i = 0; i < 44; i++)
{
test = names[i];
if (!strcmp(cmd,test))
{
index = i;
}
}
printf("%s is index of %d\n",cmd,index);}
Why do loop it for 44 times I don't get it. But when I changed 44 to 3 in the loop it works.
for (i = 0; i < 3; i++)
Depends on your compiler though but in case of 44 iteration windows gives me message application not responding.

How do I optimize C code using loop transformation? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
loop fusion, does it mean the above code will become
for(j=1; j<=4,j++){
a[j]=3;
a[j]=a[j]*2*h;
b[j]=6;
b[j]=b[j]+3*k*k;
}
Aside from using two separate loops, the original code contains lots of redundant statements. One possible improvement would be this:
int tmp = 6 * h;
for (j = 1; j <= 4; j++) {
a[j] = tmp;
b[j] = 6 + 3 * j * j;
}
The main optimizations:
One loop instead on two
Removed assignments that get overwritten in the next statement
Calculate a value that does not depend on the loop index outside the loop
You should be aware that most of these optimizations would be done by a compiler anyways.

Address is not incrementing? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
It is said that,in c,b++; is equal to b=b+1; if this is the fact test++ in my code why generate a compile time error.
test+1 is working well but test++ is not working.but why?
#include<stdio.h>
int main(void)
{
char test[80]="This is a test";
int a=13;
for(;a>=0;a--)
{
printf("%c",*(test++);
}
}
The ++ and -- operators are not defined for arrays.
v++; would be the same as v = v + 1;. Assumed v was typed an array this would imply assigning to an array, which is not defined.
char test[80] = "This is a test";
char *p = test;
for(int a = 0; a < 14; a++)
{
printf("%c", *(p++));
}
Well, for one thing, b++ is not the same as b=b+1.
But even if it were -- I think you'll find you get a similar error if you try test = test + 1.

C array print segmentation fault? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Improve this question
Why am I getting Segmentation fault? Could you give me some understadable explanation? Thanks in advance.
#include <stdio.h>
int main()
{
int i,j;
char* ips[1000];
char ip[15] = "192.34.132.52";
char port[4] = "4003";
for (i = 0; i < 10; i++) {
sprintf(ips[i], "%s:%d", ip, port);
}
for (j = 0; j < 10; j++) {
printf("[%d] = %s\n", j, ips[j]);
}
return 0;
}
You didn't allocate the memory for ips[i], sprintf doesn't do it for you. Add a line in the first for loop, before the sprintf:
ips[i] = malloc(sizeof(ip)+sizeof(port)+2);
EDIT: as huseyin tugrul buyukisik noted, port isn't big enough to hold 4 characters and a null terminator. And you should use the %s modifier for it as port is a string as well.

Unhandled exception at 0x012219c4 in SW-Serial.exe [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 8 years ago.
Improve this question
i declare 2d dynamic array. when run program this error shown:
Unhandled exception at 0x012219c4 in SW-Serial.exe: 0xC0000005: Access violation writing location 0xabababab.
the part of my program that error occured:
double** SWArray;
SWArray = (double**) malloc(lenA*sizeof(double*));
for (int i = 0; i <= lenA; i++)
SWArray[i] = (double*) malloc(lenB*sizeof(double));
for(int i=0;i<=lenA;i++){
SWArray[0][i]=0;
}
for(int j=0;j<=lenB;j++){
SWArray[j][0]=0;
}
picture of this problem
Arrays start from 0 in C. Wherever you say i <= lenA it should be i < lenA. Same goes for j and lenB. Also, the second loop doesn't really make sense. Did you mean lenB instead of lenA ?
You have lenA And lenB mixed up. It should be:
SWArray[i][0] and SWArray[0][j] in your loops.
And you loops should use < not <=
Both the loops are indexing in the wrong dimension and also are accessing elements beyond the size for which memory is allocated. You should change i<= to i< in both the loops
The correct way should be:
for(int i=0;i<lenA;i++){
SWArray[i][0]=0;
}
for(int j=0;j<lenB;j++){
SWArray[0][j]=0;
}

Resources