Create an array with increasing decimal values in C [closed] - c

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]);
}

Related

For loop not working properly to get inputs in an array in c [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 3 years ago.
Improve this question
I am working on a coding problem "find the smallest number from inputs", I take inputs in the form of for loop and store them in an array. Its working fine for total inputs less than 11 but for greater than 11 it only takes 10 inputs and then breaks.
printf("HOW MANY NUMBERS DO YOU WANT TO INPUT\n");
int array_size , var1;
scanf("%d",&array_size );
var1 = array_size;
int index = 0 , array[index];
for(int index = 0; index < array_size; index++)
{
printf("inputs left: %d\n",var1);
var1 -= 1;
scanf("%d",&array[index]);
}
I expect it should take as many inputs as user desire but it only takes 10 inputs and I can't seem to find the problem.
You want to create an array with a size of zero which is not allowed. You also canĀ“t declare an array with a variable size so you have to use malloc or something else.
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("HOW MANY NUMBERS DO YOU WANT TO INPUT\n");
int array_size;
scanf("%d", &array_size);
int* Array = (int *) malloc(array_size * sizeof(int));
for(int index = 0; index < array_size; index++)
{
printf("inputs left: %d\n", array_size - index);
scanf("%d", (Array + index));
}
for(int i = 0; i < array_size; i++)
{
printf("%d\n\r", *(Array + i));
}
free(Array);
return 0;
}
Which gives
HOW MANY NUMBERS DO YOU WANT TO INPUT
3
inputs left: 3
1
inputs left: 2
2
inputs left: 1
3
1
2
3
Or you use something like an std::vector.
#include <stdio.h>
#include <vector>
int main()
{
printf("HOW MANY NUMBERS DO YOU WANT TO INPUT\n");
int array_size;
scanf("%d", &array_size);
std::vector<int> Array;
for(int index = 0; index < array_size; index++)
{
int Temp;
printf("inputs left: %d\n", array_size - index);
scanf("%d", &Temp);
Array.push_back(Temp);
}
for(int i = 0; i < array_size; i++)
{
printf("%d\n\r", Array.at(i));
}
return 0;
}
Which result in the same output.
int index = 0 , array[index];
You're declaring an array of size 0 here. Array sizes must be positive.
You're not using index as declared here, to remove it and use array_size as the size:
int array[array_size];

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

Please explain the following C code? [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 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.

C/C++ : Why is it impossible to declare a pointer in loop initialization [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
When printing an array, initializing an integer works.
int a[MAX_SIZE] = {1,2,3,4,5,6,7,8,9,10};
for (int i = 0; i <= (MAX_SIZE - 1); i++)
{
printf("%3d",a[i]);
}
However, I wonder why initializing a pointer to an integer ("walker") won't work:
int a[MAX_SIZE] = {1,2,3,4,5,6,7,8,9,10};
for (int *aWalk = a, int *aEnd = a + MAX_SIZE - 1; aWalk <= aEnd; aWalk++)
{
printf("%3d", *aWalk);
}
The statement int *aWalk = a, int *aEnd = a + MAX_SIZE - 1; wouldn't even work on its own, so it can't work in a loop header either. The syntax you are looking for is this:
int *ptr1 = some_address, *ptr2 = some_other_address;
This works inside and outside of a loop. Also, note that your problem is not declaring one pointer but two. That's also why you are supposed to first extract a minimal example.
The for initial expression can be a definition for multiple variables as long as it is combined as a single definition:
int a[MAX_SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (int *aWalk = a, *aEnd = a + MAX_SIZE; aWalk < aEnd; aWalk++) {
printf("%3d ", *aWalk);
}
Note that it is more generic to define the end pointer to point past the end of the array as this form can handle slices of width 0.
You can do it this way only using pointer arithmetic. This is working because you are declaring an array of data and this data is continuously stored in the memory.
int a[] = {1,2,3,4,5,6,7,8,9,10};
for (int *aWalk = a; aWalk < (a + sizeof(a) / sizeof(int)); aWalk++)
{
printf("%3d", *aWalk);
}
Also try it here IDE One[^].
edit:
due to the comments I changed the code using an end pointer. code[^].
for (int *aWalk = a, *aEnd = (a + sizeof(a) / sizeof(int)); aWalk < aEnd; aWalk++)
{/*...*/}
You have an extra int in the second code. Also your placement of comma is wrong. Also removed unnecessary aEnd variable.
int a[MAX_SIZE] = {1,2,3,4,5,6,7,8,9,10};
for (int *aWalk = a; *aWalk < (a + MAX_SIZE - 1); aWalk++)
{
printf("%3d", *aWalk);
}

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