Please explain the following C code? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
When I compile and run the C code shown below, it generates the following:
Input:
#include <stdio.h>
int main()
{
int i, j;
int a, b;
for (j = 0; j <= 4; j+=2)
{
a = j;
b = 0;
for (i = 0; i <= 4; i++)
{
b += 2 * a * i;
}
printf("%d %d\n", a, b);
}
}
Output:
0 0
2 40
4 80
If anyone can tell me why the following input generates the above output, this would be much appreciated.

Hope you understand it with the simple trace table I draw.

This seems to be a basic C example showing arithmetic and printf statements.
It helps if you break down a problem like this into modules:
1) Execute the steps 2a and 2b, with j = 0, 2, 4, sequentially:
for (j = 0; j <= 4; j+=2)
2a) For each index of j, b = b + 2 * j * i (a = j here)
for (i = 0; i <= 4; i++)
{
b += 2 * a * i;
}
2b) printf("%d %d\n", a, b) is just printing out the values of j (since a is assigned the value of j) and b, where the calculations are done in step 2a.
Next time try to give the exact area where you are confused. Explaining something like this over chat is not easy. You have to break it down on your own really.

Related

Nesting For loop in c , at i = 4 ; j will be 2 how does it satisfy the condition? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
{
for (int i = 0; i <= 5; i++)
{
for (int j = 5; j >= i; j--)
{
printf("+");
}
printf("\n");
}
}
At i = 4; j will be 2 how does it satisfy the condition of j>=i?
You seem to be thinking that "j will be 2" because you see two "+" in output in that line.
But that only means that the inner loop is executed twice. Which is because (quoting Barmars comment):
When i == 4, the inner loop will only loop twice, with j == 5 and j == 4.
You analysed the detailed behaviour of your code based on output, which is good. But if something puzzles you then you either need more output on exactly the detail which puzzles you; e.g. by actually outputting the value in question for verifying your assumptions. Or you could use a debugger.
For example. I changed your code in a way which probably makes things very obvious:
#include <stdio.h>
int main(void)
{
for (int i = 0; i <= 5; i++)
{
printf("%d: ", i);
for (int j = 5; j >= i; j--)
{
printf("%d", j);
}
printf("\n");
}
}
It gets you an output (e.g. here https://www.onlinegdb.com/online_c_compiler ) of:
0: 543210
1: 54321
2: 5432
3: 543
4: 54
5: 5
Where the line 4: 54 indicates two inner loop iterations, with values for j of 5 and 4, while i is 4.

Create an array with increasing decimal values in C [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 4 years ago.
Improve this question
Super noob question here, but I haven't worked much in C and I'm trying to create an array but it doesn't seem to be working. I have played around a bit in an online compilator but I just can't get it right.
What I want is an array containing 100 elements. I want the first element to be 8, the last element to be 12, and every element should increase by 0.04. So [8, 8.04, 8.08, ..... , 11.96, 12].
Can anyone help a newbie out? :)
#define NUMS 101
int main()
{
double arr[NUMS];
double start = 8.0, end = 12.0;
double gap = (end - start) / (NUMS - 1);
int i;
for (i = 0; i < NUMS; ++i)
arr[i] = start + i * gap;
}
Here is code example based on Blaze snipped. This code first fills array and then prints it out. Also as jwismar mentioned you need 101 elements.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[]) {
float a[101];
for (int i = 0; i < sizeof(a)/sizeof(a[0]); i++)
a[i] = 8.0 + (i*0.04);
for (int i = 0; i < sizeof(a)/sizeof(a[0]); i++) {
printf("%f\n",a[i]);
}
return 0;
}
BTW if you want to start from 8 and end with 12 then you need 101 elements.
#include <stdio.h>
#include <stdlib.h>
int main()
{
double arr[101];
int i;
arr[0] = 8;
for (i = 1; i < 101; i++)
arr[i] = arr[i - 1] + 0.04;
for (i = 0; i < 101; i++)
printf("%f\n",arr[i]);
}

Arrays printing sum of values [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
I have a problem with counting the sum of values from each line in a multidimensional array in the language C.
Example:
My array with values:
1 2 3 4
5 6 7 8
9 10 11 12
My result array should be:
10
26
42
w - count lines
k - count columns
int tab[w][k]; <-- this is a table just with values(it's example)
int sum[] = {0};
int i,j;
for(i=0;i<w;i++)
{
for(j=0;j<k;j++)
{
sum[i] = sum[i] + tab[i][j];
}
}
It doesn't work well. I've tried do it another way but it only counted the first row.
Please help me, thanks.
sum[] = {0} should be sum[w];, and you should fill it with zeroes before doing the sums.
Then just sum like you did, you can do it better using +=, that works the same as your original code but is easier to write:
#include <stdio.h>
#define w 3
#define k 3
int tab[w][k] = {
{0, 1, 2},
{3, 4, 5},
{6, 7, 8}
};
int main()
{
int sum[w];
int i;
int j;
for (i = 0; i < w; i++)
{
sum[i] = 0;
}
for(i = 0 ; i < w ; i++)
{
for(j = 0 ; j < k ; j++)
{
sum[i] += tab[i][j];
}
printf("sum[%d] = %d\n", i, sum[i]);
}
}
Then your code should run fine

Prime number into an array in C [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
I try to do a function in order to sort an array and display after that only the prime number. But all the elements of my array are random numbers, and the problem is that the function display only negative prime numbers and not positive like 7 and 3, what can I do in order to solve the problem
int prime_arr(int size, int *arr, int *sort_arr)
{
int i, j, k = 0, flag;
for (i = 0; i < size; i++)
{
flag = 0;
for (j = 2; j < arr[i]/2; j++)
{
if (arr[i] % j == 0){
flag = 1;
break;
}
}
if (flag == 0){
sort_arr[k++] = arr[i];
}
}
return j;
}
I see 3 problems with the code:
1. You should return k, not j. k is the size of sort_arr
2. You should loop until arr[i] / 2, not one less than that (see <= in code below)
3. You do not handle negative numbers. Change your loop to the following:
for (j = 2; j <= abs(arr[i])/2; j++)
Without the code that prints your values, I'm not sure exactly what you're looking for, but hopefully fixing these will fix your problem.

my C code is too awkward [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 9 years ago.
Improve this question
Assume the character a,b,c,d,e represent the number 1 to 9, and they cannot be equal to
each other.
Question:
How many equals that can meet (ab * cde = adb * ce).
Example:
36 * 495 = 396 * 45.
Here is my code,and the result is right.However,i think my code is too awkward,especially in (if(a!=b&&a!=c&&a!=d&&a!=e&&b!=c&&b!=d&&b!=e&&c!=d&&c!=e&&d!=e&&c*d*e!=0))
I would appreciate it if someone could give me a better solution.
#include<stdio.h>
main(){
int a,b,c,d,e,m,n,i=0;
long f1,f2,f3,f4;
for(m=11;m<=99;m++){
a=m/10;
b=m%10;
if(a!=b&&a*b!=0)
{
for(n=101;n<=999;n++)
{
c=n/100;
d=n%100/10;
e=n%10;
if(a!=b&&a!=c&&a!=d&&a!=e&&b!=c&&b!=d&&b!=e&&c!=d&&c!=e&&d!=e&&c*d*e!=0)
{
f1=a*10+b;
f2=c*100+d*10+e;
f3=a*100+d*10+b;
f4=c*10+e;
if(f1*f2==f3*f4) i++;
printf("\n%d%d*%d%d%d*=%d%d%d*%d%d\n",a,b,c,d,e,a,d,b,c,e);
}
}
}
}
printf("%d\n",i);
return 0;
}
If you can, instead of
int a,b,c,d,e;
Try to use
int numbers[5];
And then to check if your numbers are all different, you can use for loops
doubleOccurence = FALSE; /* where FALSE = 0 */
for (i=0; i < 4; i++) {
for (j=i+1; j < 5; j++) {
doubleOccurence = doubleOccurence || (numbers[i] == numbers[j]);
}
}
It looks a bit clearer to me.
Unfortunately you can't really iterate through a list of variables you are better off with an array of numbers like Julien mentions in his answer.
int nums[5];
replace a with nums[0], b with nums[1], etc....
But then I would go one step further to tidying up your code and call a function that takes in the array to check uniqueness:
if(listIsUnique(nums, 5)) // yes hardcoded the 5, but that can be sorted
{
...
}
And then:
bool listIsUnique(int* nums, int len)
{
for (int i = 0; i < len; i++)
for (int j = i + 1; j < len; j++)
if (nums[i] == nums[j])
return false; // return false as soon as you find a match - slightly faster :)
return true; // if we get here its a unique list :)
}
Note: code is untested, there may be mistakes :o

Resources