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
Related
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]);
}
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 4 years ago.
Improve this question
Question Edited
I'm very new in C, and here.
Is it possible for an array return its value via a struct?
Although I'm trying to process each character in List[5] = {1, 2, 3, 4}, however, it only sticks at 1 and only prints 575757 rather than
My struct
struct Count numbers() {
struct Count numbers;
int List[5] = {1, 2, 3, 4};
int i = 0;
for (i = 0; i < 10; i++) { //It might be something in my for loop
numbers.intOne= List[i] + 4; // 1 + 4
numbers.intTwo= List[i] + 6; // 1 + 6
return numbers;
}
};
This only prints 575757, i wish this to print 576879
void printCode(struct Count numbers) {
int i;
for (i = 0; i < 3; i++) {
printf("%i%i", numbers.intOne, numbers.intTwo);
}
}
The main
int main() {
int i = 0;
for (i = 0; i < 10; i++) {
numbers();
printCode(numbers());
getchar();
}
}
Thank you for any help!
it only stucks at '4352'
You are returning from decode soon after processing the first element of the output array which is 4352. So the other elements of check and param arrays do not get filled up.
In any case, check and param are arrays local to the decode function and the values of their elements cannot be used outside the function.
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
So Im creating a simple for loop within a for loop. The var frequecyCount is not resetting to 0, don't understand why.
I have going through to arrays x and hist array and need a counter variable to count the frequency of the same value in x as the position of hist.
#include <stdio.h>
void create_hist(double *x, int count, int *hist) {
int frequencyCount = 0;
for (int iHist = 0; iHist <= 5; iHist++) {
//initalize to zero
hist[iHist] = 0;
for (int ix = 0; ix < count; ix++) {
// convert to int
x[ix] = (int)x[ix];
if (x[ix] == hist[iHist]) {
frequencyCount++;
}
}
// add to the hist[] array the frequency count at position i
hist[iHist] = frequencyCount;
frequencyCount = 0;
}
int main( void ) {
double x[5] = {0, 0, 0, 0, 3};
int hist[5];
int count = 5;
create_hist(x, count, hist);
printf( "\tInput data:\n" );
for ( int i = 0; i < count; i++ ) {
printf( "\t%d\t%f\n", i, x[i] );
}
printf( "\tHistogram:\n" );
for ( int i = 0; i <= 5; i++ ) {
printf( "\t%d\t%d\n", i, hist[i] );
}
return 0;
}
Try this change:
for (int iHist = 0; iHist < 5; iHist++) { // <= changed to <
int frequencyCount = 0; // Moved this line to be inside the loop
The frequencyCount variable is resetting. There is another reason your output is not what you expect.
This if statement is most likely wrong:
if (x[ix] == hist[iHist]) {
frequencyCount++;
}
At this stage, hist[iHist] is always 0 (that's the value you assigned just before the loop).
I think you mean:
if (x[ix] == iHist) {
frequencyCount++;
}
You also need to change the loop range condtion from i <= 5 to i < 5 in main and from iHist <= 5 to iHist < 5 in create_hist to avoid buffer overflow.
Making these changes results in the output:
Input data:
0 0.000000
1 0.000000
2 0.000000
3 0.000000
4 3.000000
Histogram:
0 4
1 0
2 0
3 1
4 0
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.
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 7 years ago.
Improve this question
Given the problem:
The statement is a bit ambiguous. I don't really understand what they want.
I can display the desired result using just a regular for loop:
int step = 0
for(int i = 1; i < m + 1; i++)
{
if(i != p)
{
printf("(%d, %d)", step, i);
step++;
}
}
Is this what they really want? I see that they are talking about linear time, so I think it can't be that easy? Am I supposed to build the vector that they are talking about, and then delete it?
Something like this? (compile with gcc test.c -lm)
#include <stdio.h>
#include <math.h>
int main(void) {
int i, j;
int n = 4;
int m = (int)pow(2,n);
int p = 5;
for(i = 1; i <= n; i++)
{
for(j = (int)pow(2,i-1); j < ((int)pow(2,i)); j++)
{
if(j == p) {
continue;
}
printf("%d, %d\n", i, j);
// remove vector[j]
if((j == (m-1)) && (m != p)) {
printf("%d, %d\n", i, m);
// remove vector[m]
}
}
}
return 0;
}
Run result n=3, p=5:
1, 1
2, 2
2, 3
3, 4
3, 6
3, 7
3, 8
Run result n=4, p=5:
1, 1
2, 2
2, 3
3, 4
3, 6
3, 7
4, 8
4, 9
4, 10
4, 11
4, 12
4, 13
4, 14
4, 15
4, 16
You should write the full code. Let me make it simple:
It should delete every element of the array.
By the end of the loop your program should print out the step and one of the numbers that were deleted, into the format (k, q) where k is the step you are on, and q is one of the elements you had deleted .
Every time you delete an element you can change it by X.
You also need to remember the algorithm they are telling you to use and the "rules of the game", for example: n should be positive and an integer.
And if you do not understand the algorithm, just try to put the idea on a paper:
When I put n = 1, I should delete one element from the array. When n = 2, 2 elements, When n = 3 it should delete 4 elements , n = 4 it deletes 8 element, etc.
As we can see, it is pretty simple because it goes like a geometric progression.