The meaning of [i] variable in this sitiuation? [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 3 years ago.
Improve this question
Whats the meaning of [i] in the following example?
#include <stdio.h>
int main()
{
int values[5];
printf("Enter 5 integers: ");
for (int i = 0; i < 5; ++i)
{
scanf("%d", &values[i]); // HERE
}
printf("Displaying integers: ");
for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}

i here is a variable.
In your code, [i] acts as the index of values and is used to access the element in array values.
Edit:
Since there is a //HERE comment in your code, im going to assume you would also want what [i] does there. The expression &value[i] basically gives the address of value[i] ,i.e, the "ith" element of the array.

Related

Do not understand memory surfing in C [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 4 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
#include <array.h>
int col, str;
int *point;
void setArr()
{
printf("Input columns="); scanf("%d", &col);
printf("Input strings="); scanf("%d", &str);
int num[str][col];
for(int i = 0; i < str; ++i)
{
for(int j = 0; j < col; ++j)
{
scanf("%d", &num[i][j];
}
}
point = num;
}
int main(void)
{
setArr();
printf("First=%d\n", *point);
printf("Number=%d", *point);
}
Output:
Input columns=2
Input strings=2
1
2
3
4
First=1
Number=1740639104
Here we have code in C, that have to get exact number from array using pointer, but during many attempts I understand that there is something I do not understand or just do not know.So there is a problem(or it have to be like this), namely, I refer to pointer ,which points on first element two times and I get different results in each case. Why it happened and in which way I could solve it? Thanks,everyone.
With
point = num;
you are setting point to an address of a function local variable. All further access of that will be undefined behaviour.

C - matrix from file [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 5 years ago.
Improve this question
I know that was a lot of questions and answers about "loading matrix from file in C". But I have one problem and question.
My piece of code looks like this:
for(int i=0; fgets(buffer_char, 256, file); i++) {
tmp_char = strtok(buffer_char, " \t");
for(int j=0; tmp_char != NULL; j++) {
printf("%s ", tmp_char);
strtod(tmp_char, array[i][j]);
tmp_char = strtok(NULL, " \t");
}
}
I have input file with matrix like:
3
1 2 3
4 5 6
5 6 7
I'm trying to load it into array, but I have to check if input values are correct. And my question is, How I can do it better? Which solution will be more correct?
#edit:
I modified code to this:
for(int i=0; fgets(buffer_char, 256, file); i++) {
array[i][0] = strtod(buffer_char, &tmp_char);
for(int j=1; tmp_char == NULL; j++) {
array[i][j] = strtod(tmp_char, &tmp_char);
}
}
And it works, How I want, but only for first elements of new line. I know, the problem is with "array[i][j] = strtod(tmp_char, &tmp_char);", but I have no idea what to do.
You can simply use fscanf for validating and storing your input into an array.
Code:
if((check=fscanf(fp,"%d",&n))==1)
{
int Arr[n][n];
for(int i=0; i<n; i++) {
for(int j=0;j<n;j++){
fscanf(fp,"%d",&Arr[i][j]);
printf("%d ",Arr[i][j]);
}
printf("\n");
}
}

Iterate through array in c manually [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a string array of 10 words and I want to display them.
However, I want the user to press a key and the next values are displayed.
I.e, it should display a, then the user presses a key and it displays b.
How can I achieve this?
char destination[10][10]
for (i=0; i<10; i++){
printf(Enter a name: );
scanf( "%s", name[i])
}
for (i=0; i<10; i++){
printf(" Name %d: %s", i, name[i]);
getch();
}
I'm not sure what you want, nor what your actual problem is, but this works fine here:
#include <stdio.h>
#include <conio.h>
int main() {
char name[10][10];
int i;
for (i = 0; i<10; i++) {
printf("Enter a name : ");
scanf("%s", name[i]);
}
for (i = 0; i<10; i++) {
printf(" Name %d: %s", i, name[i]);
getch();
}
}

Sum of the all the numbers between a and b [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 7 years ago.
Improve this question
I need to create a program that gives the sum of the all the numbers between the constants of a and b given by the user. b needs to be greater than a.
#include <stdio.h>
void main()
{
int index, begno, endno, sum = 0;
printf("Program for sum of all numbers in the given range\n");
printf("Enter Beg. No.: ");
scanf("%d", &begno);
printf("Enter End. No.: ");
scanf("%d", &endno);
index = begno;
for(; index <= endno; index ++)
sum = sum + index;
printf("The sum of even numbers between %d and %d is: %d", begno, endno, sum);
}
The code given looks OK, but if you want the sum without including the last number, as is generally the case you should change the for loop like this
for(; index < endno; index ++)
I would start by implementing a loop to compute:
$$\sum_{n=a}^{b}
n$$

How to enter spaces in a for loop in c language using conditions [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 9 years ago.
Improve this question
The number of spaces in each line will change as per the number that is given in the for loop. So if the number changes to 2 as per the for loop, it will have 2 spaces in that line and the number will go on increasing in every line in an ascendinfg order.
int i, j;
for(i = 0; i < 10; i++){
for(j = 0; j < i; j++) printf(" ");
printf("\n");
}
int i,j;
for (i=1; i<5; i++){
for (j=1; j<=i; j++){ // it will print i spaces whenever i increases.
printf(" ");
}
}

Resources