Why are these integer values in my array changing? - arrays

I'm trying to read in data from a file and it seems to be read in correctly, however when I print the data 2 of the values change to 3. I'm very confused why this is happening. The file that is being read in looks like this:
John 7 0 1 3 2 0 1 1
Jack 3 4 4 1
Jane 5 3 2 3 0 4
Jenny 6 4 2 1 3 0 4
Jim 2 0 0
Joanna 4 1 2 4 2
The first number is just used to identify how many following numbers there are. For the first line, the first print statement reads it in as 0132011, but when the second print statement is executed it prints out 0132441. This also happens in the line with Jenny. It is read in as 421304, but printed out as 421300.
int* philRequests[numPhilosophers];
int numRequests[numPhilosophers];
for(int i = 0; i < numPhilosophers; i++){
fscanf(fp, "%s", buff);
strcpy(names[i], buff);
fscanf(fp, "%d", &numRequests[i]);
philRequests[i] = (int *)malloc(numRequests[i] + 1); //allocates memory by the number of requests each phil will make
for(int x = 0; x < numRequests[i]; x++){
fscanf(fp, "%d", &philRequests[i][x]);
printf("\nAdding %d to %d %d", philRequests[i][x], i, x);
}
fgets(buff, 255, (FILE*)fp); //moves on to next line
}
//displaying what was just read in
for(int i = 0; i < numPhilosophers; i++){
for(int x = 0; x < numRequests[i]; x++){
printf("\nReading %d from %d %d", philRequests[i][x], i , x);
}
printf("\n");
}

Looks like an issue with overwriting memory because you aren't allocating enough space in your malloc call. Malloc allocates the specific number of Bytes you ask for. You ask for (numRequests[i]+i) Bytes. But you are actually looking for (numRequests[i]+i) pointers to int.
Try something like
philRequests[i] = malloc((numRequests[i] + 1)*sizeof(int*));

Related

Is there a way to add multiple values into an array in one command prompt?

I am trying to scanf 5 numbers into an array in one command line prompt. I know how to store a value one by one into an array, but in this case I am trying to store 5 values in a line. I know how to hard code it when the limit is clear, but I want to code it based on a user inputted limit.
for example (hard code):
int arr[5];
scanf("%d %d %d %d %d", &arr[0], &arr[1], &arr[2], &arr[3], &arr[4]);
for (i = 0; i < 5; i++) {
printf(" %d", arr[i]);
}
would give me the values of arr[0] arr[1] arr[2] arr[3] arr[4].
BUT what if the size of aaa is defined by users, or defined by a macro that allows changes? How do you not hard code it?
#define MAX 10
int arr[MAX];
or
printf("what is the limit? : ");
scanf("%d", &limit);
int arr[limit];
I tried using a for loop but it doesn't work.
for(i=0;i<MAX; i++){
scanf("%d %d %d %d %d\n", &aaa[i]); //I want user to input 5 numbers in one line, but this format doesnt work.
}
To conclude/clarify my question.
I want user to input 5 numbers in one line. for example : 1 2 3 4 5 with a space in between. and I want it stored at arr[0] arr[1] arr[2] arr[3] arr[4]. Is there a way to not hard code this?
Help would be greatly appreciated! Thanks!
You want this:
#include <stdio.h>
int main()
{
int arr[5];
printf("Enter 5 numbers separated by space then press Enter\n");
for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < 5; i++) {
printf(" %d", arr[i]);
}
}
Execution:
Enter 5 numbers separated by space then press Enter
1 2 3 4 5
1 2 3 4 5
You need to read inputs in the loop as below.
for(i=0;i<MAX; i++) {
scanf("%d", &aaa[i]);
}

Trying to make scanf not go to a new line in a for loop (C Programming)

Sorry if my English is poor. What I'm trying to do is get the scanf to be entered on the same line. For example Enter value: 1 6 8 9 4 1 2 8 5 and it to be separated by a space. Then the numbers to be stored in an array. This is my code:
#include <stdio.h>
int main(void)
{
int a[10], smallest, i;
printf("Random\n");
for (i = 0; i < 9; i++)
scanf("%d", &a[i]);
smallest = a[0];
for (i = 0; i < 9; i++)
{
if (a[i] < smallest)
{
smallest = a[i];
}
}
printf("\nSmallest Element : %d\n", smallest);
}
Thanks for any help!
Edit: I'm trying to make the user enter 9 numbers which are stored in the array using scanf but when entering the numbers the scanf goes to a new line for example:
> 5
> 6 and so on what I want is for them to enter the number numbers on the same line with a space in between like this Enter value: 1 6 8 9 4 1 2 8 5
Scanf will await for a complete line. I suggest you take your input as a string then use strtok to extract the values and then assign.
Edit: You could use the scanf like that:
scanf( "%d %d ...", &a[0], &a[1]...); //as many values you're to assign
However, I prefer the method I proposed initially. Keep in mind scanf is derived from "scan formatted". You'll also have to handle the result from the scanf, it returns the number of values successfully filled.
you can scanf all numbers in one statement: scanf("%d %d %d %d %d %d %d %d %d %d", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7], &a[8], &a[9]);
This will let you scan 10 numbers with spaces between them, without the need to hit enter every time
note that I scanned 10 ints, because your array can store 10 ints, while your loop only scans 9... (to fix it, change i < 9 to i < 10 as Sourav Ghosh suggested)

Space separated integers to array

I'm new to C, and I have an assignment where I get an input of n space separated integers, and q integers separated by new lines. I would like to save the n integers in an array a, and the q integers in another array m.
This is my code so far and it works as expected for m, but the elements in a are pretty random. How can I save the space separated integers into an array?
int main() {
// The code
int n, q;
int a[n];
int m[q];
scanf("%d %d", &n, &q);
for (int i=0; i<n; i++) {
scanf("%d", &a[i]);
}
for (int i=0; i<q; i++) {
scanf("%d", &m[i]);
}
// Troubleshooting
for (int i=0; i<n; i++) {
printf("%d ", a[i]);
}
printf("\n");
for (int i=0; i<q; i++) {
printf("%d\n", m[i]);
}
return 0;
}
For example when I input:
1 2 3 4 5 6
0
1
2
3
4
5
I get:
4 5 3 4 5 6
0
1
2
3
4
5
I'm not sure but when i run your code i get segmentation error because instead of:
int n, q;
int a[n];
int m[q];
scanf("%d %d", &n, &q);
You should first read n,q and then declare a[n],m[q] like this:
int n, q;
scanf("%d %d", &n, &q);
int a[n];
int m[q];
I tried running your code giving 6 6 for the first scanf so n=6,q=6
and after that as your example i gave input:
1 2 3 4 5 6
0
1
2
3
4
5
and it printed right.
Don't use scanf. scanf is notoriously hard to use, and it's particularly wrong for your situation because it will not distinguish between different types of whitespace (spaces versus newlines).
Instead, you should read input line-by-line using fgets and then parse each line using sscanf or strtok.

How to modify the input appearance on command window

I feel quite 'stupid' as asking this question but if anyone can show me the methods to modify the input result appeared on the command window.
Example:
I want to sort 5 numbers (1, 3, 4, 7, 5) in smallest-to-biggest order and the result on the command window must be:
input: 1 3 4 7 5 /* 1 line input */
output: 1 3 4 5 7 /* 1 line output */
Edit:
Here is my code
for (i = 0; i < 5; i++)
{
scanf("%d ", &array[i]);
}
If I use this code the result on command window must be:
1
3
4
7
5
But I want all the input number in only 1 line as:
1 3 4 7 5
So what do I have to do with my code?
Regarding to your edited question, just replace "%d " with "%d".
#include <stdio.h>
#define N 5
int main(void){
int i, j, array[N];
printf("Please enter the %d numbers.\n", N);
printf("input : ");
for(i=0;i<N;++i){
scanf("%d", &array[i]);
if(i!=0){
for(j=i;j>0 && array[j-1] > array[j];--j){
//swap array[j] and array[j-1]
int tmp = array[j];
array[j] = array[j-1];
array[j-1] = tmp;
}
}
}
printf("output : ");
for(i=0;i<N;++i){
if(i!=0)
putchar(' ');
printf("%d", array[i]);
}
putchar('\n');
return 0;
}

How to read from .txt in C

I have to read from a .txt like this (1. - txt line)
1 2
1 2 3
1 3 4
but I have to read like: "1" first line, attribute to x[0], "2" first line attribute to x[1]. I know how to do that but the problem is that I have to skip to the line 2 and do the same, but it doesn't work.
So It'd be like
x[2]=1. x[2]=2, x[3]=3, x[4]=1, x[5]=3, x[6]=4
Is there a way for me to do it???
Thanks!
Let me try to be more especific
1 2 1
2 3 1
3 4
Imagine this is a txt file where 3. 2. and 1. are first, second and third line. I have to read each number on each line and assign to a vet[MAX];
I can do it, but only with the first line. I don't know how to skip to the second one
My code
#include <stdlib.h>
int main (void)
{
char buf[1024];
int numeros[8];
FILE *fp = fopen("teste.txt", "r");
if(fp == NULL)
return EXIT_FAILURE;
while(fgets(buf, sizeof(buf), fp)) {
if(buf[0] == '\n')
continue;
sscanf(buf, "%d %d %d %d %d %d %d", &numeros[0], &numeros[1], &numeros[2], &numeros[3],&numeros[4], &numeros[5], &numeros[6]);
}
fclose(fp);
printf(" \n %d \n %d \n %d \n %d \n %d \n %d \n %d", numeros[0], numeros[1], numeros[2], numeros[3],numeros[4], numeros[5], numeros[6]);
}
the output
3
4
1
131072
2685712
302692880
4798692
Process returned 53 (0x35) execution time : 0.016 s
Press any key to continue.
And I wanted
1 2 2 1 2 3 1 3 4
Assuming your double use of x[2] is a typo, and assuming the line numbers are not really in the file, all you need is to loop doing:
fscanf(file, "%d", &x[i++]);
until it fails. So remember the check the return value, if it isn't 1 it failed to find a number to convert and store, and you should stop.
Of course, this assumes that x has space enough, and that i is initialized to 0.
Use the result of sscanf().
You will likely get 2 or 3 each loop. This means 2 or 3 int were successfully scanned. This number can then be used to determine saving the int in numeros[].
int i=0;
while(fgets(buf, sizeof(buf), fp)) {
int t[7];
int result = sscanf(buf, "%d %d %d %d %d %d %d", &t[0], &t[1], &t[2], &t[3], &t[4], &t[5], &t[6]);
if (result <= 0) break; // EOF, IO error, bad data
for (int r=0; r<result; r++) {
if (i >= sizeof(numeros)/sizeof(numeros[0[)) break; // too many
numeros[i++] = t[r];
}
}

Resources