Why is this array bugging when I declare a matrix? - c

That's the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int linhas=0, col=0, num=0, i=0, pos1[100];
int pos[100];
scanf("%d %d %d", &linhas, &col, &num);
int matriz[linhas][col];
for(i=0; i<num;i++){
scanf(" %c%d", &pos[i], &pos1[i]);
}
for(i=0;i<num;i++){
pos[i] -= 97;
}
return 0;
}
It's quite simple, I declared 2 arrays, one to store the value of a char(pos[]), and the other to store integer values(pos1[]), and it works:D.
The thing is, if I declare a matrix ex: matrix[linhas][col], my code does not really store the values of a char, and if I take it off, it starts to store normally, also, it does not matter whether if I declare the matrix right after getting the rows and colums (linhas and col) or if I declare it at the end of the code. I don't know what the problem is, and I'd appreciate any hints.

int pos[100];
scanf("%d %d %d", &linhas, &col, &num);
int matriz[linhas][col];
for(i=0; i<num;i++){
scanf(" %c%d", &pos[i], &pos1[i]);
}
The %c format specifier will read in a character, but it requires the address of a character to read it into. You pass it the address of an int.
The simplest fix is to change pos to char pos[100];. Another possible fix is this:
for(i=0; i<num;i++){
char c;
scanf(" %c%d", &c, &pos1[i]);
pos[i] = c;
}

Related

Read a list of n numbers and make an array in C

So I want to take an input such as this:
The first input tells us the size of the array and the second line contains the numbers of array like this:
input:
3
1 2 3
And I want to make an array from the second input line with a size of from the first input line.
I currently have:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int n;
scanf("%d", n);
int x[n];
int y[n];
}
But after which I get stumped.
If you have a VLA(Variable Length Array) supporting compiler(eg. GCC):
int n;
scanf("%d", &n);
int arr[n];
scanf("%d %d %d", &arr[0], &arr[1], &arr[2]);
and if not,
int n;
scanf("%d", &n);
int *arr = malloc(n * sizeof(int));
scanf("%d %d %d", &arr[0], &arr[1], &arr[2]);
This code uses the functionality of scanf to be able to take multiple delimited input.
If you have to take n inputs and not only set the size of arr to n, do this:
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &n[i]);
}
It should be apparent that VLA functionality lets you to make an array on the stack with a runtime value. Otherwise, you'll need to allocate it on the heap with malloc().
What if there are more than 3 numbers to scan. Do I need to add more
"%d" and arr[] or is there some way for it to work with any number of
numbers in the second line.
To address this point you can go with iterating over loop
for(int i=0;i<n;i++)
{
scanf("%d",&array[i]);
}

Why does my program exit after taking an input?

Consider:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
char name[100];
int number;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter %d values\n", n);
for(int i=0; i<n; i++)
{
scanf("%[^\n]s", &name);
}
}
Whenever I am entering the value of n, it just prints (Enter n values) and exits the program. The for loop never runs. It ran successfully for the first time, but after that it just exits the program.
There were some answers that said it will not print anything. I don’t want it to print just to take input n times. It is not doing that.
My aim is to take n as input and then take strings of names (like harry, robin, etc.) n number of times as input.
Your code is a little incomplete. And there are a few errors here: scanf ("%[^\n]s", &name)
Do this and everything will be fine:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main(void)
{
int n;
char name[100];
int number;
printf("Enter the value of n\n");
scanf(" %d", &n);
printf("Enter %d values\n", n);
for(int i=0; i<n; i++)
{
scanf(" %99[^\n]", name);
printf("%s\n", name);
}
return 0;
}
scanf is particularly unsuited for user input.
You probably want this:
int main() {
int n;
char name[100];
int number;
printf("Enter the value of n\n");
scanf("%d", &n);
printf("Enter %d values\n", n);
for (int i = 0; i < n; i++)
{
// the space at the beginning of "%[^\n]"
// gets rid of the \n which stays in the input buffer
scanf(" %[^\n]", name); // also there sis no 's' at the end of the "%[^\n]" specifier
printf("name = %s\n", name); // for testing purposes
}
}
But this doesn't actually make much sense because the program is asking for n names, but at each run of the for loop the previous name will be overwritten with the new name.
Also be aware that scanf("%[^\n]", name); is problematic because if the user types more than 99 characters you'll get a buffer overflow.

How to use char in scanf by using loop?

I want to make simple billing software but I don't know to fix this problem
#include <stdio.h>
int main()
{
char a[20];
int i, j, b;
i = 0;
printf("How many item you have?\n>>> ");
scanf("%d", &j);
for (int i = 0; i < j; i++)
{
printf("Type the name of item no. %d?\n>>> ", i + 1);
scanf("%c", &a);
printf("Type the item quantity?\n>>> ");
scanf("%d", &b);
}
return 0;
}
This code is only for asking questions, as you can see. In this code everything is fine but, when I run this code, the output is:
How many item you have?
>>> 4
Type the name of item no. 1?
>>> Type the item quantity?
>>>
Everything seems fine but I haven't entered the item name and the loop is asking the 2nd question directly. How is it even possible?
The %c format specifier for scanf reads a single character. To read a string (array) of characters, use the %s format specifier. Also, for such arrays, you don't need the & (address of) operator, as the array name itself will 'decay' to a pointer to its first element:
#include <stdio.h>
int main()
{
char a[20];
int i, j, b;
i = 0;
printf("How many item you have?\n>>> ");
scanf("%d", &j);
for (int i = 0; i < j; i++) {
printf("Type the name of item no. %d?\n>>> ", i + 1);
scanf("%19s", a); // The "19" limits input size and allows space for the nul-terminator
printf("Type the item quantity?\n>>> ");
scanf("%d", &b);
}
return 0;
}

Simple for loop running infinitely

I'm new to C and I need help with this simple exercise using for. I need to get a char and an int value from the user. Then I have to print that char as many times as the int entered before.
This is what I have:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a;
printf("Enter a character:");
scanf(" %c", &a);
int n;
printf("Enter a number:");
scanf(" %c", &n);
printf("\n");
int x;
for(x=0; x < n; x++){
printf(" %c", a);
}
return 0;
}
My problem is that it makes an infinite loop in the for.
Please I need your help.
Thanks
Here, n is an int, not a char. Thus, you need to use %d to read it. Using %c here will cause undefined behavior. According to "C99 – ISO 9899-1999":
§7.19.6.2 The fscanf function1
[...] If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.
Change
scanf(" %c", &n);
to
scanf(" %d", &n);
Check out here for more info.
1: The scanf function is equivalent to fscanf with the argument stdin interposed
before the arguments to scanf.

Print the nth member among five members’ height using Structure

#include <stdio.h>
struct member {
char name[20];
int age;
char sex;
int height;
};
int main(void)
{
int i,j;
struct member input[5]={0,};
int tot, rank, max=0;
int max2[5]={0,};
float res;
for (i=0; i<5; i++)
{
scanf("%s ", input[i].name);
scanf("%d %c %d", &input[i].age, &input[i].sex, &input[i].height);
getchar();
}
scanf("%d", &rank);
max =input[0].height;
for(i=1;i<=5;i++) {
for(j=0;j<5;j++) {
if(max>=input[j].height)
max=max;
else
max=input[j].height;
}
max2[i]=max;
for(j=0;j<5;j++) {
if(max==input[j].height)
input[j].height*=(-1);
}
max=-1;
}
for (i=0; i<5; i++)
{
if(max2[rank]== input[i].height)
printf("%s %d %c %d\n",input[i].name, input[i].age, input[i].sex,input[i].height);
}
fflush(stdin);
getchar();
return 0;
}
The result printed of above my code is nothing...Even the height inputted becomes negative number..
What's wrong with this program?
Input & Print should be the same like example image.......
please help!
The first scanf should be fixed
scanf("%s ", input[i].name);
In the second scanf, the input[i].sex is a char type so you have to use "%c" instead of %s
scanf("%d %s %d", &input[i].age, &input[i].sex, &input[i].height);
should be
scanf("%d %c %d", &input[i].age, &input[i].sex, &input[i].height);
You have a lot of problems here:
1) You're taking the wrong parameters from scanf():
scanf("%s ", input[i].name); // You don't need a & for a string
scanf("%d %c %d", &input[i].age, &input[i].sex, &input[i].height); // you need %c for a
// character
2) You're using for(i=1;i<=5;i++) to access an array of 5 elements, by doing this you're overflowing the array (it should be 0 to 4, not 1 to 5)
3) This is a nit-pick but:
if(max>=input[j].height)
max=max;
that is totally pointless. You don't need to set a variable to itself, just invert the logic (<) and only do the else case.
4) You set all the input[x].height's to the negative of the value they're originally set to here:
input[j].height*=(-1);
Then you check to see if that is the same as the original values which you stored in max2[] before printing here:
if(max2[rank]== input[i].height)
printf("%s %d %s %d\n",input[i].name, input[i].age, input[i].sex,input[i].height);
Well, that's never going to happen, so you'll never print anything
5) fflush(stdin); is not a well defined operation on most systems and could lead to undefined behavior, so don't do it.

Resources