A looping statement in C [closed] - c

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 8 years ago.
Improve this question
I have this array and I can't seem to figure out how to form a for looping statement to be able to display these values in a list.
float array1[] = {30 , 0.81 , 0.16 , 30 , 2813 , 58 , 8.4 , 0.61};
I tried figuring it out but it just kept being a syntax error
for (array1; array1 <= 8; array1++ )
printf("%f\n", array1);
any suggestions??

It should be:
for(int i = 0; i < sizeof(array1)/sizeof(float); i++)
printf("%f\n", array1[i]);

array1 is the name of the array and not a variable that You want to increment!
int i = 0;
for (i = 0; i < 8; i++ ) printf("%f\n", array1[i]);

You need an index into your array:
int i;
for (i = 0; i < SIZE; ++i) { // SIZE should be 8.
printf("%f\n", array1[i]);
}
Think of your array as a contiguous section of memory that contains SIZE blocks of data. The index i is used to access the value stored in each block.

int i = 0;
for (i; i <= 8; i++)
printf("%f\n", array1[i]);

int length = sizeof(array1) / sizeof(float);
for(int i = 0; i< length; i++)
{
printf("%f\n", array1[i]);
}

try the following code,
float *ind;
for (ind=array1; ind < array1+8; ind++ ){
printf("%f\n", *ind)
}

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

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.

The int array displays an odd value at the end [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
I am running this code for teaching purposes about sizeof and displaying array values; everything looks OK with the char array (the last element of the array '0' or NULL is not displayed, the float array displays it as 0.0000... but the char array gives me the last element of 54417138 that looks like an address (hex equivalent 206F6554). I tried to retrieve the content of it assuming it is an address (see the comment part of the code) but got a stack overflow error so I am lost at this moment - any help will be appreciated.
#include <stdlib.h>
int main(void) {
int i =0;
int j = 0;
int k = 0;
char name[] = {'T','e','o',' ','G','u','g' };
int z = sizeof(name)/sizeof(name[0]);
printf("%d\n", z);
for ( k=0; k <= z; k++) {
printf("%c", name[k]);
}
for ( k=0; k <= z; k++) {
printf("%c", *(name+k));
}
printf("\n");
int number[] = {1,2,3,4,5,6,7,8 };
int x = sizeof(number)/sizeof(number[0]);
printf("%d\n", x);
for ( i=0; i <= x; i++) {
printf("%d\t", number[i]);
}
for ( i=0; i <= x; i++) {
printf("%d\t", *(number+i));
}
printf("\n");
/*int *ptr = 0x206F65540;
printf("Memory address is: %i\n", ptr);
printf("Content of that address is: %d\n", *ptr);
printf("\n");*/
float number1[] = {1.0, 2.5, 3.1234, 10.10};
int y = sizeof(number1)/sizeof(number1[0]);
printf("%d\n", y);
for (j=0; j <= y; j++) {
printf("%f\t", number1[j]);
}
for (j=0; j <= y; j++) {
printf("%f\t", *(number1+ j));
}
printf("\n");
return EXIT_SUCCESS;
}
This is the output of the program:
7
Teo Gug Teodor Gug
8
1 2 3 4 5 6 7 8 544171348 1 2 3 4 5 6 7 8 544171348
4
1.000000 2.500000 3.123400 10.100000 0.000000 1.000000 2.500000 3.123400 10.100000 0.000000
note the strange value in the display of int array of 544171348*
You are accessing beyond the end of your arrays with this kind of loop
for ( i=0; i <= x; i++) {
should be
for ( i=0; i < x; i++) {
^
^
as array indices are zero based.
Reading beyond the end of your arrays is undefined behaviour.

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

Error in sentences in bold: " Subscripted value is neither array nor pointer vector :(" [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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
#include <stdio.h>
void main()
{
int arrsize;
int randset;
int max;
int min;
int arr[arrsize];
int i, j;
int swap;
float mean;
printf("Input array size:\n");
scanf("%d", &arrsize);
printf("Input random set:\n");
scanf("%d", &randset);
printf("Input maximum possible value:\n");
scanf("%d", &max);
printf("Input minimum possible value:\n");
scanf("%d", &min);
// SORTING //
for (i = arrsize; i > 0; i--)
for(j = 1 ; j < i ; j++)
// Subscripted value is neither array nor pointer vector //
**if(arrsize[j-1] > arrsize[j])**
{
swap = arr[j];
arr[j] = arr[j-1];
arr[j-1] = swap;
}
for (i=0; i < arrsize; i++)
printf("%d", arr[i]);
printf("\n\n");
// MEAN //
for(i = 0 ; i < arrsize ; i ++)
{
//Subscripted value is neither array nor pointer vector //
**mean += arrsize[i];**
mean/= arrsize;
printf("The mean is %.2lf\n\n", mean);
}
// MEDIAN //
if(arrsize%2 == 0)
{
printf("The median is %.2lf", (float)(arr[arrsize/2 -1] + arr[arrsize/2]);
}
else
{
printf("The median is %d", arr [arrsize/2]);
printf("\n\n\n");
}
printf("The midrange is %.2lf\n\n", (float)(arr[0] + arr[arrsize - 1] / 2);
}
void fillintarray(int myarray [], int arrsize, int min, int max, unsigned int randset)
{
int iter;
srand (randset);
for (iter =0 ; iter < arrsize ; iter++)
{
myarray[iter] = rand() % (max - min + 1) + min;
}
}
Hi there! I put the lines where there were errors in bold, and the error was
"Subscripted value is neither array nor pointer vector"
This is an exercise for my ComSci class :) Help would be much appreciated! Thanks! :)
Well, the error message is pretty descriptive - you accessing a scalar variable int arrsize with subscript operator [] which could only be applied to array or pointer type.
You probably want to check j-1-th and j-th array elements, you should use
if (arr[j-1] > arr[j])
You're confusing between the array(arr) and its size(arrsize).
If you don't know the size of array , use malloc .
Use int *array = malloc(arraysize * sizeof(int));instead of int array[arraysize];
And put it afterscanf("%d",&arraysize);
And you also need init the array before use it.

Resources