Getting an error when trying to fill an array with Strings and ints in MIPS? - arrays

So I need to fill an array in MIPS with 10 "records", which consist of an employee's name, age, and salary. The employee's name is a max of 40 characters and both the age and salary are supposed to be integers. However, when testing this loop I wrote, I always get an "invalid integer input (syscall 5)" for line 18 after I go through the first 5 employees and I type "Emp6" and press enter and I can't seem to figure out why. Any help would be greatly appreciated!
Test Input:
Emp1
1
1
Emp2
2
2
Emp3
3
3
Emp4
4
4
Emp5
5
5
Emp6
Error Code:
Error in line 18: Runtime exception at 0x00400020: invalid integer input (syscall 5)
Go: execution terminated with errors.

Your main problem seems to be that the read_string system call expects the buffer size in $a1, but you use that for your loop counter. Thus, you eventually run out of space, and there will be unprocessed text left in the input buffer which will then be read by the subsequent read_int and that blows up.
Further problem is that you keep incrementing $a0 but fail to account for that in your addressing. Also sw $v0, 0($a0) doesn't make much sense, the string has been read into memory already, $v0 has nothing interesting in it.

Related

How to process array data in chunks where last chunk may be a partial size

I have array of integer and I am trying to send this array as a sub block from esp32 to another one.
According this code I get on output like this:
output:
1 2 3 4 5
6 7 8 9 10
11 12 0 0 0
the expected output:
1 2 3 4 5
6 7 8 9 10
11 12
How can I update on esp_now_send to get like the expected output? how can I deal with the last sub block if it is less than 5 numbers?
The code needs to send up to only the available data. To do that the general approach would be to send full sub-blocks until the last sub-block which may be a partial one. That can be determined by simple maths logic to work out how much the current iteration should send based on how much data is left.
The code changes would be:
Change siz to be the real number of entries in the array: siz = sizeof(data)/sizeof(data[0]).
Change rang in the function call to `(ind + rang <= size ? rang : size - ind)``. That is, the size passed to the function call depends on how much data is left.

Chocolate Feast Program

I am trying to solve the Chocolate Feast challenge on HackerRank:
Little Bob loves chocolate, and he goes to a store with $N in his pocket. The price of each chocolate is $C. The store offers a discount: for every M wrappers he gives to the store, he gets one chocolate for free. How many chocolates does Bob get to eat?
Input Format:
The first line contains the number of test cases, T.
T lines follow, each of which contains three integers, N, C, and M.
Output Format:
Print the total number of chocolates Bob eats.
Constraints:
1≤T≤1000
2≤N≤105
1≤C≤N
2≤M≤N
Sample input:
3
10 2 5
12 4 4
6 2 2
Sample Output:
6
3
5
Explanation
In the first case, he can buy 5 chocolates with $10 and exchange the 5 wrappers to get one more chocolate. Thus, the total number of chocolates is 6.
In the second case, he can buy 3 chocolates for $12. However, it takes 4 wrappers to get one more chocolate. He can't avail the offer and hence the total number of chocolates remains 3.
In the third case, he can buy 3 chocolates for $6. Now he can exchange 2 of the 3 wrappers and get 1 additional piece of chocolate. Now he can use his 1 unused wrapper and the 1 wrapper of the new piece of chocolate to get one more piece of chocolate. So the total is 5.
Here is my attempt at a solution in C:
#include<stdio.h>
int main(){
int t; //total test cases
scanf("%d",&t);
for(int a0 = 0; a0 < t; a0++){
int n; //money
int c; //cost of 1 chocolate
int m; //no of wrappers to buy a new chocolate
scanf("%d %d %d",&n,&c,&m);
int tc=0,nw=0,nc=0,w=0;//tc=totalChocolates nw=newWrappers nc=newChocolates w=wrappers
tc=n/c;
w=tc;
while(w>=m){
nc=(w/m);
tc+=nc;
w-=m;
nw=w%m;
w+=nw;
}
printf("%d\n",tc);
}
return 0;
}
The problem is that my program passes some test cases whereas it fails in some others, but I am not able to find where the error is.
Moreover for some other test the time taken is above 2 secs.
Test case
Input
Excepted output
Your logic here is rather muddled:
while(w>=m){
nc=(w/m);
tc+=nc;
w-=m;
nw=w%m;
w+=nw;
}
If you change it to this then it passes all the test cases:
while(w>=m){
nc=(w/m); // how many additional bars can we buy ?
tc+=nc; // accumulate total bars purchased
w-=(nc*m); // deduct no of wrappers used to purchase additional bars
w+=nc; // accumulate additional wrappers
}
This program (before editing) outputs 10 instead of 9 for this input.
1
5 1 2
Try this in the while loop:
Subtract m*nc instead of m from w
Add nc instead of nw to tc

Accesing two different rows simultaneously in C

Suppose I have a data set arranged in the following way
19 10 1 1
12 15 1 1
13 12 4 5
10 5 2 3
...
and so on, at a particular iteration in a for loop I have to read only the 1st and the 4th row and in the next iteration I have to access some other set of rows,for example
1st iteration:
1st row: 19 10 1 1
4th row: 10 5 2 3
i will access my data using the fscanf() function. But how will i ensure that I choose only the 1st and 4th rows or any two rows for that matter at a given iteration?
(I have not considered reading it into a 2D array since the size of data set is 10^8 )
Thank you.
As you read through your data (say, stored in a standard file), get byte offsets for rows by looking for row delimiters (a newline character). You can then read out rows based on the start and end byte offset with C pointer arithmetic on a FILE * and fseek(). Storing a few byte offsets (an eight byte long or equivalent, often) is cheap.

C: how to store integers by reading line by line with a random layout?

I need to read a file and store each number (int) in a variable, when it sees \n or a "-" (the minus sign means that it should store the numbers from 1 to 5 (1-5)) it needs to store it into the next variable. How should I proceed?
I was thinking of using fgets() but I can't find a way to do what I want.
The input looks like this:
0
0
5 10
4
2 4
5-10 2 3 4 6 7-9
4 3
These are x y positions.
I'd use fscanf to read one int at a time, and when it's negative, it is obviously the second part of a range. Or is -4--2 a valid input?

Declaring more than one SPIM array causes a syntax error

Below is the beginning of a chunk of SPIM code:
.data
a: .space 20
b: .space 20
.text
set_all:
sw $ra,0($sp)
li $t0,0
li $t1,10
............
Unfortunately, the second array I declare ('b') causes the SPIM interpreter to spit out:
spim: (parser) syntax error on line 3
of file spim.out b: .space 20
^
Similar code works when I only have one array -- it seems to be the second that screws it up. I've prodded at it but can't figure out what it is about that statement that makes it break. Any thoughts? Thanks for any insight.
/facepalm
After poking around a bit more, I remembered that 'b' is a reserved word in SPIM. It stands for 'branch'. Hoo boy.

Resources