with Ada.Text_IO; use Ada.Text_IO;
procedure factor is
rate: Integer;
begin
Put("Enter rate: ");
Get(rate);
case rate is
when 1 | 2 =>
Put("Factor =", factor = 2 * rate - 1);
when 3 | 5 =>
Put("Factor =", factor = 3 * rate + 1);
when 4 =>
Put("Factor =", factor = 4 * rate - 1);
when 6 | 7 | 8 =>
Put("Factor =", factor = rate - 2);
when others =>
Put("Factor =", rate);
end case;
end factor;
I am new to ada and im not able to resolve this error. please help me with this code
this is a simple switch case in ada.. this should return factor and rate is the input from user
Here is a comparison of your program and one that compiles and works correctly:
---------------------------------------------------------------
-- Original program:
--
-- with Ada.Text_IO; use Ada.Text_IO;
-- procedure factor is
-- rate: Integer;
-- begin
-- Put("Enter rate: ");
-- Get(rate);
-- case rate is
-- when 1 | 2 =>
-- Put("Factor =", factor = 2 * rate - 1);
-- when 3 | 5 =>
-- Put("Factor =", factor = 3 * rate + 1);
-- when 4 =>
-- Put("Factor =", factor = 4 * rate - 1);
-- when 6 | 7 | 8 =>
-- Put("Factor =", factor = rate - 2);
-- when others =>
-- Put("Factor =", rate);
-- end case;
-- end factor;
------------------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure Main is
rate : Integer;
factor : Integer;
begin
Put ("Enter rate: ");
Get (rate);
case rate is
when 1 | 2 =>
factor := 2 * rate - 1;
when 3 | 5 =>
factor := 3 * rate + 1;
when 4 =>
factor := 4 * rate - 1;
when 6 | 7 | 8 =>
factor := rate - 2;
when others =>
factor := rate;
end case;
Put_Line ("Factor =" & factor'Image);
end Main;
In Ada I/O procedures and functions are tied to the types being read or written. Therefore, reading an Integer requires an instance of Ada.TextIO.Integer_IO. The predefined package Ada.Integer_Text_IO is just such a package. The Get procedure from that package is used to input the value for the rate variable.
In Ada variables must be declared before they are used and they are only declared in the declarative part of a procedure or function; that is, the part of the procedure or function before the reserved word begin. The variable named factor is declared in the declarative part of the procedure main.
The case statement calculates the value of factor based upon the rate input. After the end of the case statement the variable factor has been assigned a value according to the logic in the case statement. It is then output with a trailing newline character using the Put_Line procedure.
The Put_Line procedure requires a single string for its argument. That string is created by concatenating a literal string "Factor =", with the string representation of the value in factor. The 'Image attribute converts the integer value in factor to its corresponding string representation. That string value is concatenated with the literal string forming a single string argument for the Put_Line procedure.
First of all, you did not mention the error you got.
I think you got this first
factor.adb:8:05: no candidate interpretations match the actuals:
factor.adb:8:05: missing argument for parameter "Item" in call to "get" declared at a-textio.ads:239
factor.adb:8:05: missing argument for parameter "Item" in call to "get" declared at a-textio.ads:205
factor.adb:8:09: expected type "Standard.String"
factor.adb:8:09: found type "Standard.Integer"
If you carefully read, you'll see that the compiler complains about the types. It expected Standard.String and got Standard.Integer.
In fact, you used Ada.Text_io which is for text only (cf. ARM).
As you want to get an integer, you must use the integer version for IO i.e. Integer_IO.
For displaying, there are several ways but you can use Integer'Image(my_int)
to convert your integer to a string.
Then check where the variable factor is declared. In fact, you never declared it but don't even have to do so.
Finally check how you can concatenate two strings, you will be able to join the "Factor =" with the integer converted to string.
When you like to get rate from the user it is good to know what rate is.
From the case statement it looks like a rate type could be declared as -
1) type Rate_Type is 1 .. 8;
2) type Rate_Type is new Integer range 1 .. 8;
3) subtype Rate_Type is Integer range 1 .. 8;
Rate : Rate_Type;
In this case (new to Ada) option 3 would be the best option. So now Rate is an Integer in the range from (including) 1 to (including) 8.
To get Rate from the user
package Rate_IO is new Ada.Text_IO.Integer_IO (Rate_Type);
The package Rate_IO is like Ada.Integer_Text_IO but for Rate_Type instead of Integer and Rate is input from the user
Rate_IO.Get (Rate);
The advantage of Rate_IO is that if the user enters a value not in the range of Rate_Type an exception (Data_Error) is raised.
This will cause the program to stop with some message.
It is often a better choice to stop the program than to let it run with a value out of range. At least it makes the program much simpler.
So now that Rate is in range, the case statement looks like
case rate is
when 1 | 2 => factor := 2 * rate - 1;
when 3 | 5 => factor := 3 * rate + 1;
when 4 => factor := 4 * rate - 1;
when 6 | 7 | 8 => factor := 1 * rate - 2;
end case;
Below is a program with an exception handler. And uncommented is an example with a default value for rate.
with ada.text_io; use ada.text_io;
with ada.integer_text_io;
procedure program_2 is
subtype rate_t is integer range 1 .. 8;
package rate_io is new ada.text_io.integer_io (rate_t);
rate : rate_t;
factor : integer;
begin
-- Input
put ("Enter rate: ");
rate_io.get (rate); -- May raise data_error exception
-- Calculate
case rate is
when 1 | 2 => factor := 2 * rate - 1;
when 3 | 5 => factor := 3 * rate + 1;
when 6 | 7 | 8 => factor := 1 * rate - 2;
when 4 => factor := 1 * rate + 0;
end case;
-- Output
put ("factor =");
ada.integer_text_io.put (factor);
new_line;
-- Error handling
exception when data_error =>
put_line ("rate out of range");
end program_2;
-- Put begin/exception/end block around rate_io.get to
-- provide ie a default value:
--
-- begin
-- rate_io.get (rate);
-- exception when data_error =>
-- rate := rate_t'first; -- A default value
-- end;
I want to print array data user input.
But it give me error. I need to get input from user and store in the array. After this i want to print it also count each duplicate word.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Main is
type String_Val is array (1 .. 100) of Character;
type Int_Val is array (1 .. 100) of Integer;
Data : String_Val;
begin
Put_Line ("Please enter values (use space as seperator)");
for I in 1..String_Val'Length loop
Data(I) := Character'Value(Get_Line);
end loop;
for I in String_Val'Range loop
Put (String_Val (Data));
end loop;
end Main;
The following program reads a string as input, tallies the occurrences of characters in the string and outputs both the input string and the occurrences of each character in the input string.
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
type Character_Counts is array (Character) of Natural;
Input : String (1 .. 100); -- String is an array of Character;
Counts : Character_Counts := (Others => 0);
Length : Natural;
begin
Put_Line("Enter a string:");
Get_Line(Item => Input,
Last => Length);
Put_Line ("You entered:");
-- output the data entered by the user
Put_Line(Input(1..Length));
-- Count character occurrences in the string
for I in 1 .. Length loop
Counts(Input(I)) := Counts(Input(I)) + 1;
end loop;
-- output counts of characters in the string
for C in Counts'Range loop
if Counts(C) > 0 then
Put_Line(Character'Image(C) & " : " & Counts(C)'Image);
end if;
end loop;
end Main;
Type Character_Counts is an array of Natural indexed by all the values of type Character. Natural is a pre-defined subtype of integer with a minimum value of 0. The variable Counts is an instance of Character_Counts with all its elements initialized to 0.
The Get_Line procedure returns the string you passed in, filled with characters up to the length of the string or the number of characters entered. The variable Length contains the number of characters entered by the user.
When counting the occurrences of the characters in the string the each character of the string is used as an index into the variable Counts, and the corresponding element in Counts is incremented.
A sample execution of this program is:
Enter a string:
This is a test.
You entered:
This is a test.
' ' : 3
'.' : 1
'T' : 1
'a' : 1
'e' : 1
'h' : 1
'i' : 2
's' : 3
't' : 2
i search for a special kind of query in SQLite
to sort a notes table.
The result from the query should be like this:
id oid
1 1
2 1,1
5 1,1,a
6 1,1,a,1
3 1,1,A
4 1,1,A,1
But with the folling code I receive this:
CREATE TABLE note (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
oid VARCHAR unique,
tit VARCHAR,
dsc VARCHAR
);
select id, oid from note
order by oid collate NOCASE
Result:
id oid
1 1
2 1,1
5 1,1,a
3 1,1,A
6 1,1,a,1
4 1,1,A,1
Any suggestions?
Thanks
--jonah
The following transforms the sort keys so that the normal case sensitive ordering yields the requested result:
If there was a togglecase() function, the function would uppercase lowercase and lowercase uppercase (for example Hello => hELLO), one could ORDER BY togglecase(oid) and the result would be in the order requested.
You could define such a function and expose it to SQLite as a UDF. It could also be possible to write this function using builtin SQLite functions but I don't know them well enough to give an answer using them. The following is an example of such a function in Python:
def togglecase(s):
def toggle(l):
if l.isupper():
return l.lower()
if l.islower():
return l.upper()
return l
return ''.join(toggle(l)
for l in s)
Note that for proper Unicode support it needs to iterate over graphemes. Not over code points.
See that this does what I described it to do:
>>> togglecase("1,1,A")
'1,1,a'
>>> togglecase("1,1,a")
'1,1,A'
It is possible to test if this sorts correctly in Python:
>>> sorted(["1", "1,1", "1,1,a", "1,1,a,1", "1,1,A", "1,1,A,1"])
['1', '1,1', '1,1,A', '1,1,A,1', '1,1,a', '1,1,a,1']
See how the uppercase follows the lowercase:
>> sorted(["1", "1,1", "1,1,a", "1,1,a,1", "1,1,A", "1,1,A,1"], key=togglecase)
['1', '1,1', '1,1,a', '1,1,a,1', '1,1,A', '1,1,A,1']
Now if you use it in SQLite like:
SELECT id, oid
FROM note
ORDER BY togglecase(oid)
This should result in:
1 "1"
2 "1,1"
3 "1,1,a"
4 "1,1,a,1"
5 "1,1,A"
6 "1,1,A,1"
The code is untested except for the togglecase function.
You are getting that result because the sorting is pecified to be NOCASE. That means that "a" and "A" are equals. So, first the rows with "a/A" and nothing after, and then rows with "a/A" and data after.
If you make the query CASE SENSITIVE, you will get a different result. BUT "A" comes befores "a" in case sensitive sort:
SELECT id, oid
FROM note
ORDER by oid
Results:
1 "1"
2 "1,1"
5 "1,1,A"
6 "1,1,A,1"
3 "1,1,a"
4 "1,1,a,1"
I have an ID field that is just numbers but the ID's vary in length. If the number is 16 characters long then I need to display 'x' + last 4 of the ID, if not then just display the last 10 of the ID.
If your ID field is already a string:
CASE character_length([ID])
WHEN 16 THEN 'x' || substring([ID],character_length([ID]) - 3)
ELSE substring([ID],character_length([ID]) - 9)
END
If your ID is stored as an integer, I'd recommend creating a new data item that casts it as a varchar (say named 'ID String'):
CAST([ID],VARCHAR(16))
Then substitute the new value in the first expression:
CASE character_length([ID String])
WHEN 16 THEN 'x' || substring([ID String],character_length([ID String]) - 3)
ELSE substring([ID String],character_length([ID String]) - 9)
END
My array A may be returns array of age likes
int [] A = {11,12,13} or
int [] A = {14,15} or
int [] A = {11,14}
My Person table has column likes
ID Name Age
---------------------
1 John 12
2 Michael 15
3 Tom 13
4 Owen 14
How can I get row from this table depend on my array's values using lambda ?
myASPGridView.DataSource = DBContext.Persons.Where(.....);
It should be something like that:
int [] A = {11,12,13};
myASPGridView.DataSource = DBContext.Persons.Where(p => A.Contains(p.Age));
Do you want this?
x => A.Contains(x.Age)
x denotes the input (in this case, your database row) and the function returns true where the array A contains x.Age.