Computer Programming, Data Structures & Agricultural Informatics

Agricultural Engineering · Foundations · also Agricultural Informatics & AI, Computer Programming and Data Structures

8units
11chapters
8,255words
126practice questions

Programming, data and the basics of AI as used in agricultural engineering, from spreadsheets to decision support systems.

In the exams: Core in the ICAR 6th Deans' Committee B.Tech; ICAR AIEEA-PG Unit II (fundamentals of computers)

Syllabus

  1. Algorithms and flowcharts
  2. Programming in C or Python
  3. Data structures
  4. Spreadsheets for engineering calculations
  5. Databases
  6. Introduction to artificial intelligence and machine learning
  7. Decision support systems
  8. Agricultural apps and IoT data

Study manual

Computer programming, data structures and agricultural informatics is the subject in which an agricultural engineer learns to make a computer do engineering work: turn a design procedure into an algorithm, write it as a program, store field and laboratory data so it can be found and trusted, and use statistical learning and decision support tools without being fooled by them. The ICAR 6th Deans' Committee B.Tech curriculum places it in the first year (Agricultural Informatics and Artificial Intelligence, then Computer Programming and Data Structures), because every later course, from hydrology to machinery testing, assumes the student can compute.

The manual follows the eight units of the course: algorithms and flowcharts; programming in C or Python; data structures; spreadsheets for engineering calculations; databases; an introduction to artificial intelligence and machine learning; decision support systems; and agricultural apps and IoT data. Numerical methods are taught inside the programming chapters because that is where agricultural engineers use them: solving Manning's equation for normal depth, integrating a hydrograph or drying curve, fitting a calibration line. Code is shown in Python, which most universities now teach first, with C noted wherever the two languages behave differently, because C remains the language of microcontrollers and many older courses.

Worldwide this subject is examined in two ways. Entrance and postgraduate papers such as ICAR AIEEA-PG (Unit II, fundamentals of computers) ask short, exact questions: what a flowchart symbol means, what a code fragment prints, which data structure is last-in-first-out, what a primary key is, how precision differs from recall. Licensing and practice, as in the NCEES PE Agricultural and Biological Engineering specification (computer modelling, statistics, sensors and data acquisition), test whether you can use computed results responsibly. Both are served here: exact definitions and traced code for the first, judgement about data quality, model validity and decision support for the second.

1. 1. Computers, algorithms and flowcharts

What a computer does and how numbers are stored

A digital computer has four functional parts: input devices, output devices, memory, and a central processing unit (CPU) made of an arithmetic-logic unit and a control unit. Primary memory (RAM) is fast and volatile, so it loses its contents when power is removed; secondary storage (solid-state or magnetic disks) is slower and non-volatile. Read-only memory holds start-up firmware. Software is divided into system software (the operating system, compilers, device drivers) and application software (a spreadsheet, a GIS, a farm record app).

Everything is stored as bits, each 0 or 1; eight bits make a byte. An n-bit unsigned integer represents 0 to 2^n − 1, so one byte holds 0 to 255. Signed integers almost always use two's complement, in which an n-bit word holds −2^(n−1) to 2^(n−1) − 1; an 8-bit signed integer therefore runs from −128 to 127. To form the two's complement of a number, invert every bit and add one. Converting between bases is examined often: binary 1011 is 8 + 0 + 2 + 1 = 11 in decimal; each hexadecimal digit stands for exactly four bits, so binary 1111 0101 is F5 in hexadecimal, 245 in decimal. Decimal 13 becomes binary 1101 by repeated division by 2, reading remainders from last to first.

Real numbers are stored in floating point. Nearly all modern hardware follows IEEE 754, whose double-precision format uses 64 bits and gives about 15 to 17 significant decimal digits. Many decimal fractions, such as 0.1, have no exact binary representation, so 0.1 + 0.2 is stored as 0.30000000000000004 and a test for exact equality between computed floats fails. Engineers compare floats with a tolerance instead: abs(a − b) < 1e-9. Storage prefixes have two conventions: in SI usage a kilobyte is 1000 bytes, while the binary prefix kibibyte (KiB) is 1024 bytes; say which you mean.

Algorithms and their properties

An algorithm is a finite sequence of unambiguous steps that turns given input into the required output. Knuth's classic list of properties is still the one examined: finiteness (it stops after a finite number of steps), definiteness (each step is precisely defined), input (zero or more quantities supplied), output (one or more results) and effectiveness (each step is basic enough to be carried out exactly in finite time). A recipe that says "add salt to taste" fails definiteness; a loop with no exit fails finiteness.

Algorithms are written first in pseudocode or as flowcharts, then coded. Structured programming rests on the result, usually credited to Böhm and Jacopini, that any computable procedure can be built from three control structures: sequence (steps one after another), selection (if-then-else) and iteration (a loop). Avoiding unrestricted jumps makes programs easier to read and to prove correct.

Efficiency is described by order of growth, written in Big-O notation: the number of basic operations as a function of input size n, ignoring constant factors. Linear search through an unsorted list is O(n); binary search of a sorted list halves the remaining range at each step and is O(log n), so a sorted list of 1,000,000 items needs at most 20 comparisons. Simple sorts (bubble, selection, insertion) are O(n²) in the worst case; merge sort is O(n log n) in every case; quicksort is O(n log n) on average but O(n²) in its worst case.

Flowcharts and pseudocode

A flowchart draws an algorithm with standard symbols, defined internationally in ISO 5807. The terminal symbol, an oval or rounded rectangle, marks start and stop. A rectangle is a process (a calculation or assignment). A parallelogram is input or output. A diamond is a decision with one entry and two or more labelled exits, such as yes and no. A rectangle with double vertical sides is a predefined process, a subroutine described elsewhere. A small circle is an on-page connector that joins flow lines without drawing them across the page; flow lines carry arrowheads showing the direction of control. A loop appears in a flowchart as a flow line returning from a decision to an earlier point.

Pseudocode writes the same logic in structured plain language, independent of any programming language:

READ n total ← 0 FOR i ← 1 TO n READ rain[i] total ← total + rain[i] END FOR PRINT total / n

Tracing means executing an algorithm by hand, keeping a table of every variable after each step. Tracing is how examiners test understanding of loops: the student must state the final value of a variable or the number of times a loop body runs. A loop "FOR i ← 1 TO n" runs n times; a loop that starts at 0 and continues while i < n also runs n times; one that continues while i <= n runs n + 1 times. These off-by-one errors are the most frequent bug in student code, so count them deliberately.

Remember

2. 2. Programming fundamentals: data types, operators and expressions

Compilers, interpreters and the two teaching languages

A program in a high-level language must be translated into machine instructions. A compiler translates the whole source file before it runs and reports syntax errors at compile time; C is normally compiled, and its build has stages: preprocessing (handling #include and #define), compilation to object code, and linking with libraries into an executable. An interpreter translates and executes statement by statement. The standard Python implementation compiles source to an intermediate bytecode and then interprets that bytecode, so Python errors in a branch that never runs may never be reported.

The two languages differ in typing. C is statically typed: every variable is declared with a type (int, float, double, char) before use, and the compiler checks assignments. Python is dynamically typed: a name refers to an object, and the object carries its type, so the same name may refer to an integer and later to a string. Python is strongly typed in the sense that "3" + 4 raises a TypeError rather than silently converting.

Errors fall into three groups. Syntax errors break the grammar of the language and stop translation. Run-time errors occur during execution, such as division by zero, an index outside a list, or opening a missing file. Logical errors produce wrong answers without any message, such as using diameter where the formula needs radius. Logical errors are the dangerous ones in engineering, and the defence is testing against a hand-calculated case.

Data types and arithmetic

The basic types are integers, floating-point numbers, characters or strings, and Boolean values. In C an int is commonly 32 bits on current desktop compilers, but the standard guarantees only a minimum of 16 bits, so its size is implementation-defined; a char is one byte; float is usually single precision (about 7 significant digits) and double is double precision. Python's int has arbitrary precision, limited only by memory, so 2**100 is computed exactly; Python's float is a C double.

Division is the most examined trap. In Python, / always gives a float (7 / 2 is 3.5), // is floor division (7 // 2 is 3) and % gives the remainder (7 % 2 is 1). Floor division rounds towards minus infinity, so −7 // 2 is −4 and −7 % 2 is 1, the remainder taking the sign of the divisor. In C, dividing two integers gives an integer: 7 / 2 is 3. Since the C99 standard, integer division truncates towards zero, so −7 / 2 is −3 and −7 % 2 is −1, the remainder taking the sign of the dividend. Writing 7.0 / 2 or casting (double)7 / 2 gives 3.5 in C. A classic C bug is averaging integer readings with sum / n and losing the fraction.

Operator precedence follows mathematics: exponentiation (Python **) before multiplication, division and remainder, which come before addition and subtraction; operators of equal precedence are applied left to right, except Python's ** which groups right to left, so 2 ** 3 ** 2 is 2 ** 9 = 512. In C there is no exponent operator; pow(x, y) from math.h is used, and the ^ symbol is bitwise exclusive-or, so 2 ^ 3 in C is 1, not 8.

Strings, input and output

A string is a sequence of characters. In Python, strings are immutable objects indexed from 0, so for s = "sorghum", s[0] is "s", s[-1] is "m" (negative indices count from the end) and the slice s[1:4] is "org" (start included, stop excluded). len(s) gives 7. In C a string is an array of char ending with the null character '\0', so "sorghum" occupies 8 bytes; forgetting room for the terminator is a classic buffer-overflow error.

Input from the keyboard arrives as text. Python's input() always returns a string, so a number must be converted with int() or float() before arithmetic; "12" + "3" is "123" while int("12") + int("3") is 15. In C, scanf("%d", &n) reads an integer into n and needs the address of the variable, written with &. Output uses print() in Python, with formatted strings such as f"{q:.2f}" to show two decimal places, and printf in C with format specifiers %d for int, %f for float or double, %c for char and %s for strings.

Remember

3. 3. Control flow, functions and arrays

Selection and loops

Selection chooses between paths. In Python the if, elif, else chain is tested from the top, and only the first true branch runs, so the order of conditions matters. A program classifying soil electrical conductivity must test the highest class first if it uses ">=" thresholds; testing ec >= 2 before ec >= 8 would put every saline sample in the lowest class. C uses if, else if, else, and also a switch statement for selecting among integer constants, where each case needs a break or execution falls through to the next case.

Iteration repeats a block. A for loop over range(n) in Python runs n times with the index taking 0 to n − 1; range(a, b) runs from a to b − 1, and range(a, b, s) steps by s. A while loop repeats as long as its condition is true and is used when the number of repetitions is unknown in advance, as in an iterative solver that stops when the error falls below a tolerance; always add a maximum iteration count so a non-converging case cannot run for ever. In C the for loop has three parts, for (i = 0; i < n; i++), and a do-while loop always executes its body at least once because the test is at the end.

break leaves the innermost loop immediately; continue skips the rest of the current iteration and starts the next. Python also allows an else clause on a loop, which runs only if the loop ended without break, useful in a search that must report "not found".

Nested loops multiply: an outer loop of 5 and an inner loop of 4 execute the inner body 20 times. An inner loop whose range depends on the outer index, such as for j in range(i), runs 0 + 1 + 2 + … + (n − 1) = n(n − 1)/2 times, which is why simple sorting algorithms are O(n²).

Functions, parameters and scope

A function packages a computation under a name so it can be tested once and reused. In Python, def manning_q(n, A, R, S): return A * R**(2/3) * S**0.5 / n defines a function with four parameters; calling manning_q(0.025, 1.2, 0.4, 0.001) passes four arguments and returns the discharge. A function without a return statement returns None in Python; in C such a function is declared void.

Parameter passing is examined in both languages. C passes arguments by value: the function receives a copy, so changing it inside does not change the caller's variable. To let a function change the caller's variable, C passes a pointer (the variable's address), as in swap(&a, &b). Arrays in C are an exception in practice: an array name passed to a function becomes a pointer to its first element, so the function can modify the caller's array. Python passes references to objects. Rebinding a parameter name inside the function does not affect the caller, but mutating a mutable object such as a list (appending, assigning an element) is visible to the caller. A related trap is a mutable default argument, def f(x, acc=[]), whose list is created once and shared between calls.

Recursion is a function calling itself, with a base case that stops the chain. Factorial(n) = n × factorial(n − 1) with factorial(0) = 1 is the standard example. Every recursive call uses stack memory, so deep recursion can overflow; Python sets a default recursion limit of about 1000 calls. Recursive Fibonacci without memoisation repeats work and grows exponentially, a useful warning that an elegant algorithm is not always an efficient one.

Arrays and lists

An array stores many values of one type in contiguous memory, accessed by an index. C arrays have fixed size, declared as double rain[365]; valid indices run from 0 to 364, and C does not check bounds, so writing rain[365] corrupts other memory without warning. The address of element i is the base address plus i times the element size, which is why access by index takes constant time. A two-dimensional array such as double yield[10][8] is stored row by row (row-major order) in C, so element [i][j] lies at offset i × 8 + j elements from the start.

Python's list is a dynamic array that can grow, holds objects of any type, and checks bounds, raising IndexError for a bad index. Useful operations include append (amortised constant time), len, slicing, sum, min, max and sorted. Lists are mutable; tuples are immutable sequences, used for fixed records such as a (latitude, longitude) pair. For large numerical work, array libraries store numbers in typed contiguous blocks and run whole-array operations in compiled code, which is much faster than an explicit Python loop.

Remember

4. 4. Numerical methods in code

Errors, roots and convergence

Numerical methods replace an exact analytical answer with an approximation computed in finite steps. Two kinds of error arise. Round-off error comes from finite precision storage. Truncation error comes from the method itself, such as replacing a curve by straight segments. True relative error is |true − approximate| / |true|; when the true value is unknown, iterative methods use the approximate relative error between successive iterations, |x_new − x_old| / |x_new|, as the stopping test.

Root finding solves f(x) = 0, as in finding normal depth from Manning's equation, the friction factor from the Colebrook equation, or the equilibrium moisture content from a sorption model. The bisection method starts from an interval [a, b] in which f changes sign, evaluates the midpoint, and keeps the half that still contains the sign change. It always converges for a continuous function with a bracketed root, but slowly: the interval halves each step, so reducing an interval of width L to a tolerance ε needs n ≥ log2(L/ε) iterations; reducing a width of 1 to 0.001 needs 10 iterations.

The Newton–Raphson method uses the tangent: x_new = x − f(x)/f'(x). Near a simple root it converges quadratically, roughly doubling the number of correct digits at each step, but it needs the derivative, can diverge from a poor starting guess, and fails when f'(x) is zero. For f(x) = x² − 2 starting at x = 1, the iterates are 1.5, 1.416667 and 1.414216, approaching √2 = 1.414214. The secant method replaces the derivative by a finite difference through the last two points, and the false-position method keeps a bracket like bisection while using a secant line.

Integration, differentiation and differential equations

Numerical integration is used for runoff volume from a hydrograph, area under a drying curve, and energy from a power trace. The trapezoidal rule with n equal strips of width h gives I ≈ h/2 [f0 + 2(f1 + … + f(n−1)) + fn]; its error falls with h², so halving h cuts the error about four times. Simpson's 1/3 rule fits parabolas through pairs of strips: I ≈ h/3 [f0 + 4(f1 + f3 + …) + 2(f2 + f4 + …) + fn]. It needs an even number of strips, its error falls with h⁴, and it is exact for polynomials up to cubic. For f(x) = x² on [0, 2] with h = 0.5, the trapezoidal rule gives 2.75, Simpson's rule gives 2.6667, and the exact value is 8/3. Simpson's 3/8 rule needs the number of strips to be a multiple of three.

An ordinary differential equation dy/dt = f(t, y) with a known starting value is an initial-value problem: thin-layer drying, dM/dt = −k(M − Me), and reservoir routing are agricultural examples. Euler's method steps y(n+1) = y(n) + h f(t(n), y(n)); it is first order, so its global error is proportional to h, and too large a step can make the solution oscillate or blow up. The classical fourth-order Runge–Kutta method evaluates the slope four times per step and has global error proportional to h⁴, which is why it is the default in many engineering programs. For dy/dt = −0.5y with y(0) = 1, one Euler step of h = 0.5 gives 0.75, against the exact e^(−0.25) = 0.7788.

Remember

5. 5. Data structures

Linear structures: arrays, linked lists, stacks and queues

A data structure is a way of organising data so the operations a program needs are efficient. An abstract data type states what operations are allowed; the data structure is how they are implemented.

An array gives constant-time access by index but costly insertion in the middle, because later elements must shift. A singly linked list stores each element in a node with a pointer to the next node; inserting or deleting after a known node is constant time, but reaching the k-th element means walking k links, which is O(n). A doubly linked list adds a pointer to the previous node, allowing traversal both ways at the cost of extra memory. In C, linked lists are built with structs and pointers allocated by malloc and released by free; forgetting free causes a memory leak.

A stack is last-in, first-out (LIFO). Its operations are push (add on top), pop (remove from top) and peek. Stacks hold function call frames, undo histories and the evaluation of postfix expressions; checking that brackets are balanced is the textbook use. Pushing 3, 5 and 7 and then popping twice returns 7 then 5. Popping an empty stack is underflow; pushing onto a full fixed-size stack is overflow.

A queue is first-in, first-out (FIFO): enqueue at the rear, dequeue from the front. Queues model waiting lines, such as trucks at a grain intake pit, and buffer sensor messages awaiting transmission. A circular queue reuses array slots by wrapping indices with the modulo operator, so a queue in an array of size N uses positions (rear + 1) % N. A priority queue serves the most urgent element first and is usually implemented with a binary heap, which inserts and removes in O(log n). A deque allows insertion and removal at both ends.

Trees, heaps and graphs

A tree is a hierarchical structure of nodes with one root and no cycles. Each node except the root has one parent; a node with no children is a leaf. The depth of a node is its number of edges from the root. A binary tree allows at most two children per node, so level i (root at level 0) holds at most 2^i nodes, and a tree of height h holds at most 2^(h+1) − 1 nodes. A tree with n nodes has n − 1 edges.

Traversals visit every node: preorder visits node, left subtree, right subtree; inorder visits left, node, right; postorder visits left, right, node; level-order visits level by level using a queue. In a binary search tree (BST) every key in a node's left subtree is smaller and every key in the right subtree is larger, so an inorder traversal lists keys in sorted order. Search, insertion and deletion take time proportional to the height: O(log n) when the tree is balanced, but O(n) when keys arrive already sorted and the tree degenerates into a chain. Self-balancing trees keep the height logarithmic, and the B-tree, a wide balanced tree, is how databases index tables on disk.

Hashing, searching and sorting

A hash table stores key-value pairs in an array, placing each pair at an index computed from the key by a hash function. Look-up, insertion and deletion take constant time on average, but collisions, when two keys hash to the same slot, must be handled, either by chaining (a list at each slot) or by open addressing (probing for another slot). Performance degrades as the table fills, measured by the load factor, items divided by slots, and tables are resized when it grows too high. Python's dict and set are hash tables: membership testing in a set is average O(1), against O(n) for a list, which matters when checking a million sensor identifiers.

Sorting arranges records by key. Bubble sort repeatedly swaps adjacent out-of-order pairs; after the first pass the largest element is at the end. Selection sort repeatedly selects the minimum of the unsorted part and makes at most n − 1 swaps. Insertion sort builds the sorted part one element at a time; it is O(n) on data that are already nearly sorted, which makes it good for small or almost-ordered sets. Merge sort splits, sorts halves recursively and merges in O(n log n) time, needs extra memory, and is stable. Quicksort partitions around a pivot, is fast in practice, and degrades to O(n²) with consistently bad pivots. A sort is stable if records with equal keys keep their original order, which matters when sorting farm records by village after they were sorted by date.

Remember

6. 6. Spreadsheets for engineering calculations

Cells, references and formulas

A spreadsheet is a grid of cells, each addressed by column letter and row number, such as B4. A cell holds a constant or a formula; formulas begin with an equals sign and recalculate whenever an input changes, which makes the spreadsheet the most widely used engineering calculation tool in agriculture. The function names discussed here, such as SUM, AVERAGE, IF and VLOOKUP, are common to the major spreadsheet programs, though details differ between programs, so check the help of the one you use.

References are the core idea. A relative reference such as A2 changes when the formula is copied: a formula =A2*B2 in C2, copied to C3, becomes =A3*B3. An absolute reference fixes both column and row with dollar signs, $B$1, and does not change when copied; it is used for a constant such as a crop coefficient or a unit conversion factor kept in one cell. Mixed references fix one part only: $A2 fixes the column, A$2 fixes the row. A multiplication table filled from one formula =$A2*B$1 uses both kinds. Named ranges, such as giving cell B1 the name Kc, make formulas self-explanatory.

Typical engineering functions include SUM, AVERAGE, MIN, MAX, COUNT, COUNTIF and SUMIF for conditional totals, STDEV for sample standard deviation, ROUND, SQRT, EXP, LN, PI, the logical IF, AND and OR, and lookup functions that return a value from a table. SUMPRODUCT multiplies two ranges element by element and adds the products, which gives a weighted mean in one formula, for example the area-weighted crop coefficient of a farm.

Building trustworthy engineering sheets

Lay a sheet out in three clearly separated blocks: inputs (coloured, with units in the adjacent column), calculations, and outputs. Never type a constant inside a formula; put it in a labelled input cell and reference it absolutely. Keep one quantity per cell and one unit per column. Document sources in a note: which equation, which standard, which table.

Test a sheet like code. Reproduce one hand-worked example exactly; change one input and confirm the output moves in the expected direction; try extreme inputs (zero area, zero slope) and confirm the sheet fails visibly rather than silently. A circular reference, a formula that depends on its own result, is usually an error, although iterative calculation can be switched on deliberately for implicit equations; when that is done, say so in the sheet.

Lookups need care. An exact-match lookup returns an error when the key is missing, which is what you want for a crop name. An approximate-match lookup on a table sorted in ascending order returns the row for the largest key not greater than the search value, which is right for banded tables such as tariff slabs but wrong for names; in several major programs approximate match is the default when the last argument is omitted.

For implicit equations, a goal-seek tool varies one input cell until a formula cell reaches a target, which solves Manning's equation for normal depth or a loan equation for the interest rate. A solver add-in handles several variables with constraints, as in a least-cost feed or crop-plan linear programme. Charts should plot measured values as points and fitted models as lines, label axes with quantities and units, and avoid three-dimensional effects that distort comparison.

Remember

7. 7. Databases

Why a database, and the relational model

A file of records serves one program; a database serves many users and programs from one shared, controlled store. A database management system (DBMS) is the software that defines, stores, retrieves and protects the data. Compared with scattered files and spreadsheets, a DBMS reduces redundancy (the same farmer's address stored in five places, four of them out of date), enforces integrity rules, controls concurrent access, and provides backup and recovery.

The relational model, proposed by E. F. Codd in 1970, stores data in relations, which are tables. Each row (tuple) is one record; each column (attribute) has a name and a domain of allowed values. The number of columns is the degree of the relation; the number of rows is its cardinality. A candidate key is a minimal set of attributes that identifies each row uniquely; the one chosen is the primary key, which may not be null. A foreign key is an attribute in one table that refers to the primary key of another, and referential integrity requires every foreign-key value to match an existing primary key, or be null where that is allowed. A soil-test table may carry field_id as a foreign key referring to the field table, so a test cannot be recorded for a field that does not exist.

Database design usually starts with an entity-relationship (ER) model: entities (farmer, field, crop season, soil sample), their attributes, and relationships with cardinalities. One farmer owns many fields (one-to-many); a field grows many crops over seasons and a crop is grown on many fields (many-to-many). A one-to-many relationship is implemented by putting the foreign key on the "many" side; a many-to-many relationship needs a separate linking table, here a crop_season table holding field_id, crop_id, season and yield.

Normalisation

Normalisation organises tables to remove redundancy and the update anomalies it causes. An insertion anomaly is being unable to record a fact without an unrelated one (no way to store a new pump model until one is sold); a deletion anomaly is losing a fact when deleting another (deleting the last sale deletes the pump model's rating); an update anomaly is having to change a value in many rows and missing some.

First normal form (1NF) requires every attribute to hold atomic values: no lists in a cell, no repeating groups such as crop1, crop2, crop3 columns. A field record with a cell "wheat, mustard" violates 1NF; the fix is one row per field-crop pair.

Second normal form (2NF) applies to tables with a composite key: every non-key attribute must depend on the whole key, not part of it. In a table keyed by (field_id, season) that also stores field_area, the area depends on field_id alone, a partial dependency; it belongs in the field table.

Third normal form (3NF) removes transitive dependencies: a non-key attribute must not depend on another non-key attribute. If a farmer table stores village and the village's district, district depends on village, not on the farmer; move village and district to their own table. Boyce–Codd normal form (BCNF) is a stricter version in which every determinant must be a candidate key.

SQL, transactions and indexes

Structured Query Language (SQL), standardised as ISO/IEC 9075, is used by almost every relational DBMS, with small dialect differences. Its data definition language (DDL) creates and changes structure: CREATE TABLE, ALTER TABLE, DROP TABLE. Its data manipulation language (DML) works on rows: SELECT, INSERT, UPDATE, DELETE. Data control statements, GRANT and REVOKE, manage permissions.

A query reads:

SELECT district, AVG(yield_t_ha) AS mean_yield FROM crop_season JOIN field ON crop_season.field_id = field.field_id WHERE crop = 'wheat' GROUP BY district HAVING COUNT(*) >= 5 ORDER BY mean_yield DESC;

WHERE filters rows before grouping; HAVING filters the groups after aggregation; ORDER BY sorts the result, ascending by default. Aggregate functions are COUNT, SUM, AVG, MIN and MAX. COUNT(*) counts rows, while COUNT(column) skips nulls. An inner join returns only rows with matches in both tables; a left outer join keeps every row of the left table and fills missing matches with null, which is how to list fields that have no soil test. A null means unknown, so the comparison x = NULL is never true; test with IS NULL. An UPDATE or DELETE without a WHERE clause changes every row in the table.

A transaction is a unit of work that must succeed or fail as a whole, such as moving stock from one warehouse record to another. Transactions have the ACID properties: atomicity (all or nothing), consistency (from one valid state to another), isolation (concurrent transactions do not see each other's partial work) and durability (once committed, the change survives a crash). COMMIT makes changes permanent; ROLLBACK undoes them.

An index, usually a B-tree, speeds search on a column at the cost of slower writes and extra storage. Index primary keys, foreign keys and the columns most often searched, such as the timestamp of sensor readings. Beyond relational systems, time-series databases store dense sensor streams, spatial extensions store field boundaries as geometry, and non-relational document stores hold loosely structured records such as app survey forms.

Remember

8. 8. Introduction to artificial intelligence and machine learning

What AI and machine learning are

Artificial intelligence (AI) is the field of building computer systems that perform tasks normally requiring human intelligence: perceiving, reasoning, planning, learning and acting. Two broad traditions exist. Symbolic or knowledge-based AI encodes human expertise as explicit rules and facts, as in expert systems for pest diagnosis. Machine learning (ML) instead learns patterns from data: rather than writing the rule, the engineer supplies examples and an algorithm fits a model. Deep learning is the part of ML that uses neural networks with many layers, and it dominates image, speech and text tasks.

ML problems fall into three main kinds. Supervised learning uses labelled examples, each an input with its known answer. If the answer is a category (diseased or healthy leaf; weed or crop) the task is classification; if it is a number (yield in t/ha; moisture content) the task is regression. Unsupervised learning has no labels and looks for structure: clustering groups similar items, such as dividing a field into management zones from yield, soil conductivity and elevation layers; dimensionality reduction, such as principal component analysis, compresses many correlated variables into a few. Reinforcement learning trains an agent by reward and penalty through interaction with an environment, a method studied for robot control and irrigation policy. Semi-supervised learning combines a few labelled examples with many unlabelled ones, useful because expert labelling of crop images is expensive.

Core algorithms

Linear regression fits y = b0 + b1x1 + … by least squares. Logistic regression, despite its name, is a classifier: it passes a linear combination of features through the logistic (sigmoid) function 1/(1 + e^(−z)) to give a probability between 0 and 1, and a threshold, commonly 0.5, turns it into a class.

k-nearest neighbours (k-NN) classifies a new point by majority vote among the k closest training points, usually by Euclidean distance. It has no training phase beyond storing data, but prediction is slow on large sets, and because distances mix units, features must be scaled first; otherwise a variable measured in large numbers, such as rainfall in millimetres, swamps one measured in small numbers, such as pH.

A decision tree splits the data by asking questions about features (is NDVI below 0.4?), choosing at each node the split that most reduces impurity, measured by Gini impurity or entropy. Trees are easy to explain but overfit when grown deep. A random forest trains many trees on bootstrap samples with random subsets of features and averages their votes, which reduces variance and usually improves accuracy. Support vector machines find the boundary with the widest margin between classes and handle non-linear boundaries through kernel functions. Naive Bayes applies Bayes' theorem assuming features are independent given the class; it is fast and works surprisingly well on text.

An artificial neural network is built from neurons, each computing a weighted sum of inputs plus a bias and passing it through a non-linear activation function, such as the sigmoid or the rectified linear unit, ReLU(z) = max(0, z). Networks are trained by gradient descent: the loss (error) is computed, its gradient with respect to every weight is found by backpropagation, and each weight is moved a small step, set by the learning rate, against its gradient. Too high a learning rate makes training diverge; too low makes it slow.

k-means clustering, the standard unsupervised method, picks k centres, assigns each point to its nearest centre, moves each centre to the mean of its points, and repeats until assignments stop changing. It minimises the within-cluster sum of squared distances, needs k chosen in advance, and is sensitive to scaling and to the starting centres.

Training, validation and overfitting

A model must be judged on data it did not see during training. The available data are split into a training set (to fit parameters), a validation set (to tune hyperparameters and choose among models) and a test set (used once, at the end, to report expected performance). A common split is about 70/15/15 or 80/10/10, but the right split depends on how much data exist.

Overfitting is learning the noise in the training data: training error is low but test error is high. Underfitting is a model too simple to capture the pattern: both errors are high. This is the bias-variance trade-off. A simple model has high bias and low variance; a very flexible one has low bias and high variance. Remedies for overfitting include more data, simpler models, regularisation (penalising large weights, as in ridge or lasso regression), early stopping, and, in neural networks, dropout.

k-fold cross-validation divides the data into k parts, trains on k − 1 and tests on the remaining one, rotating k times, and averages the scores; it gives a steadier estimate when data are scarce. In agriculture the split must respect structure. Images of the same plant, or readings from the same field and season, are strongly correlated; if some go into training and some into testing, the test score is inflated by leakage. Split by field, farm, season or site, so the test truly represents new conditions. Data leakage also occurs when information about the answer sneaks into the features, for example using harvest-time measurements to "predict" yield.

Feature scaling puts variables on comparable ranges. Min-max normalisation maps x to (x − min)/(max − min), giving 0 to 1; standardisation maps x to (x − mean)/standard deviation. Compute the scaling constants on the training set only and apply the same constants to validation and test data.

Remember

9. 9. Model evaluation and image-based crop and disease detection

Classification metrics

For a two-class problem the confusion matrix counts true positives (TP, diseased leaves called diseased), false positives (FP, healthy leaves called diseased), false negatives (FN, diseased leaves called healthy) and true negatives (TN). From it:

Accuracy = (TP + TN)/(TP + TN + FP + FN), the share of all predictions that are right. Precision = TP/(TP + FP), the share of positive calls that are truly positive. Recall, also called sensitivity or true positive rate = TP/(TP + FN), the share of real positives that were found. Specificity = TN/(TN + FP), the share of real negatives correctly cleared. F1 score = 2 × precision × recall/(precision + recall), the harmonic mean of the two.

Example: a disease detector tested on 200 leaves gives TP = 40, FN = 10, FP = 20, TN = 130. Accuracy is 170/200 = 0.85; precision 40/60 = 0.667; recall 40/50 = 0.80; specificity 130/150 = 0.867; F1 = 0.727.

Accuracy misleads when classes are imbalanced. If 95 per cent of leaves are healthy, a model that calls every leaf healthy scores 95 per cent accuracy and finds no disease. Use precision, recall and F1, and report the confusion matrix itself. Which error matters more is an agronomic decision: for an early warning of a quarantine pest, missing a case (FN) is worse, so recall is favoured; for a spot sprayer, false alarms (FP) waste chemical, so precision matters.

Classifiers usually output a score, and the threshold that turns score into class trades precision against recall. The receiver operating characteristic (ROC) curve plots true positive rate against false positive rate (1 − specificity) as the threshold varies, and the area under it (AUC) summarises ranking quality: 0.5 is chance, 1.0 is perfect. For many classes, report per-class metrics and their macro average (simple mean over classes) as well as overall accuracy.

Regression metrics

For numeric predictions, such as yield or moisture content, the residual is the observed minus the predicted value. Mean absolute error (MAE) is the average of the absolute residuals. Root mean square error (RMSE) is the square root of the mean of squared residuals; it is in the units of the target and penalises large errors more than MAE does, so RMSE is never smaller than MAE for the same data. Mean bias error, the average signed residual, shows systematic over- or under-prediction. The coefficient of determination R² = 1 − SSres/SStot compares the model's squared error with that of simply predicting the mean; it can be negative on test data for a model worse than the mean.

Example: observed yields 4.0, 5.0, 6.0 and 7.0 t/ha against predictions 4.5, 4.8, 6.3 and 6.4 t/ha give residuals −0.5, 0.2, −0.3 and 0.6. MAE = 1.6/4 = 0.40 t/ha; RMSE = √(0.74/4) = 0.430 t/ha; mean bias error = 0.0; and with SStot = 5.0, R² = 1 − 0.74/5.0 = 0.852.

Image-based crop and disease detection

A digital image is a grid of pixels; a colour image usually has three channels (red, green, blue), each an integer from 0 to 255 in 8-bit images. A 224 × 224 colour image therefore holds 224 × 224 × 3 = 150,528 numbers. Older plant-image methods extracted hand-designed features, such as colour indices, lesion area after thresholding, or texture measures, and fed them to a classifier such as an SVM. The excess green index, 2G − R − B, is a classic way to separate vegetation from soil.

Convolutional neural networks (CNNs) learn the features themselves. A convolution layer slides small filters, for example 3 × 3, across the image, each producing a feature map that responds to a pattern such as an edge or a spot; early layers find edges and colours, deeper layers find lesion shapes and leaf structures. For an input of width W, a filter of size F, padding P and stride S, the output width is (W − F + 2P)/S + 1. Pooling layers reduce the spatial size, a 2 × 2 max pool halving width and height, and final fully connected layers produce class scores, converted to probabilities by a softmax function.

Because labelled crop images are scarce, transfer learning is standard: a network pre-trained on a large general image collection is fine-tuned on the crop images. Data augmentation, meaning random flips, rotations, crops and brightness changes, enlarges the effective training set. Tasks differ: image classification gives one label per image; object detection draws boxes round each fruit or weed; semantic segmentation labels every pixel, which allows lesion area to be measured.

The main lesson of published work is that accuracy on laboratory-style images, single leaves on plain backgrounds, does not transfer to field photographs with mixed backgrounds, shadows, several diseases and different cameras; reported accuracy has fallen sharply when such models were tested on images from other sources. Field validation on images from the target region, crop stage and camera is essential, along with a route for uncertain cases to reach a human expert. Interpretability tools that highlight the image regions driving a prediction help check that the model looks at lesions rather than at the background.

Remember

10. 10. Decision support systems and expert systems

What a decision support system is

A decision support system (DSS) is an interactive computer system that helps a person make a semi-structured decision by combining data, models and the user's judgement. It supports the decision; it does not replace the decision-maker. Examples in agricultural engineering include irrigation scheduling tools, machinery selection and costing tools, fertiliser recommendation systems built on soil-test data, pest forecasting and spray advisories driven by weather, and watershed planning tools that compare conservation options.

The classic DSS has three components. The data management component holds the database: weather, soil, crop, prices and field records. The model management component holds the model base: water balance, crop growth simulation, cost equations, optimisation such as linear programming. The dialogue component, the user interface, lets the user pose questions, change assumptions and read results. A knowledge component, rules from experts, is added in knowledge-driven systems.

DSS are commonly grouped as data-driven (built around querying and summarising large data, as in dashboards of yield and price data), model-driven (built around simulation or optimisation models, as in a crop simulation used to compare sowing dates), knowledge-driven (built on expert rules), document-driven and communication-driven. The defining activity is "what-if" analysis: changing an input, such as the irrigation interval, and seeing its effect on the output, such as predicted yield and water use. Sensitivity analysis asks which inputs most change the answer, and goal-seeking asks what input value achieves a target.

Expert systems and rules

An expert system captures the knowledge of human specialists in a narrow domain and applies it to new cases. Its parts are the knowledge base, which holds facts and rules, usually in IF-THEN form; the inference engine, which applies the rules to the facts of a case; the user interface; an explanation facility that shows which rules led to a conclusion; and a knowledge acquisition facility for adding or editing rules. The working memory holds the facts of the current consultation. The person who elicits knowledge from experts and encodes it is the knowledge engineer.

Inference engines reason in two directions. Forward chaining is data-driven: it starts from known facts, fires every rule whose conditions are satisfied, adds the conclusions as new facts, and continues until nothing new can be concluded. It suits monitoring and configuration, such as deriving all advisories from today's weather. Backward chaining is goal-driven: it starts from a hypothesis, such as "the crop has a particular leaf blight", and works back to check whether the facts needed to support it are true, asking the user for missing facts. It suits diagnosis.

Uncertainty is handled with certainty factors, probabilities through Bayesian networks, or fuzzy logic, where membership in a set such as "soil is wet" is a degree from 0 to 1 instead of yes or no. Fuzzy controllers have been used for greenhouse climate and dryer control.

Remember

11. 11. Agricultural apps, IoT and sensor data

IoT architecture and communication

The Internet of Things (IoT) connects sensors and actuators to networks so their data can be collected and acted on remotely. In agriculture it covers soil moisture networks, automatic weather stations, tank and canal level sensors, storage temperature and humidity monitors, livestock collars, machine telemetry and remotely switched pumps and valves.

A widely taught three-layer model divides IoT into the perception layer (sensors, actuators and the microcontroller that reads them), the network layer (communication from the device to the internet) and the application layer (storage, analysis, dashboards, alerts and apps). Many texts add a middleware or processing layer between network and application. Edge computing processes data on or near the device, for example averaging readings or detecting a threshold locally, which saves energy and bandwidth and keeps working when the connection drops; cloud computing provides large storage and heavy analysis.

Communication choice is a trade-off among range, data rate and power. Short-range personal-area radios and wireless local-area networks carry high data rates over tens of metres; cellular networks give wide coverage where signal exists and cost subscription fees; low-power wide-area networks send small packets a few times an hour over several kilometres on battery power for years, which suits soil and weather sensors spread over farms; satellite links serve remote sites at higher cost. Application protocols include HTTP, the request-response protocol of the web, and MQTT, a lightweight publish-subscribe protocol in which devices publish messages on named topics to a broker and subscribers receive them, which suits intermittent, low-bandwidth links.

From sensor to number: acquisition and data quality

A sensor converts a physical quantity to an electrical signal; an analogue-to-digital converter (ADC) turns that signal into an integer. An n-bit ADC divides its input range into 2^n levels, so a 10-bit converter has 1024 levels and, over 0 to 5 V, a step of 4.88 mV if resolution is taken as range/2^n (some texts divide by 2^n − 1, giving 4.89 mV; the difference is small but examiners may specify one). Resolution is not accuracy: a high-resolution reading from an uncalibrated sensor is precisely wrong.

The sampling theorem requires a sampling rate greater than twice the highest frequency present in the signal to reconstruct it; slower sampling causes aliasing, in which a fast variation masquerades as a slow one. For slow agricultural variables, such as soil moisture, hourly or even less frequent readings are adequate, while vibration or engine signals need thousands per second.

Calibration relates raw output to the true quantity. Capacitance soil moisture sensors respond to soil type, salinity and temperature, so a soil-specific calibration against gravimetric samples improves them greatly; a calibration line fitted by least squares converts raw counts to volumetric water content. Record the calibration and its date with the data.

Quality control should be automatic and documented. Range checks reject values outside physical limits (relative humidity above 100 per cent, negative rainfall). Rate-of-change checks catch spikes. Persistence checks catch a sensor stuck on one value. Cross-checks compare related sensors, such as two nearby rain gauges. Missing data must be marked as missing, never as zero: a zero rainfall is a measurement, a gap is not. Timestamps should be stored in a single stated time zone, preferably coordinated universal time, with the local offset noted, because daylight-saving changes and logger clock drift otherwise create duplicate or missing hours. Every reading should carry the sensor identifier, the units and a quality flag.

Agricultural apps and data governance

Mobile apps put information and services in farmers' and extension workers' hands: weather and market price alerts, crop advisories, pest identification from photographs, machinery custom-hiring bookings, soil-test reports, digital field records and traceability for export markets. An app that works for farmers is designed around their constraints. It must work offline or with intermittent connections and synchronise later; use local languages, icons and voice where literacy varies; run on low-cost phones with little storage; and keep inputs few, because every extra field reduces use.

A typical app architecture has a mobile client, a back-end server exposing an application programming interface (API), and a database. An API is a defined set of requests a program can make to another program, usually over HTTP with data exchanged in JSON, a text format of name-value pairs and lists. APIs let an advisory app pull weather forecasts, soil maps or prices from other services instead of copying the data.

Farm data raise questions of ownership, consent and privacy. Field boundaries, yields and machine logs are commercially sensitive, and personal data about farmers are protected by data-protection law in many countries, with rules that differ by jurisdiction. Good practice, found in voluntary agricultural data codes of practice in several regions, is that farmers know what is collected, consent to its use, can obtain their data in a usable format, and can ask for deletion. Data should be encrypted in transit and stored securely, devices should not ship with default passwords, and firmware should be updatable, because an insecure pump controller is a physical risk as well as a data risk.

Remember

What the exams ask

How it is used, across a career

As a student

A first-year student uses this subject in every laboratory and design course that follows. Lab reports need a script or spreadsheet that reads measurements, converts units and computes means, regressions and errors without retyping numbers. Hydrology assignments need a loop over daily rainfall records; machinery courses need a cost sheet with absolute references for fixed rates; the numerical methods taught here solve Manning's equation for normal depth and integrate drying curves. For entrance and PG examinations, the habits that score are tracing code by hand with a table of variables, knowing exact definitions (LIFO, primary key, 3NF, precision, recall), and remembering the integer-division and indexing rules of C and Python. Final-year projects increasingly use sensor kits and small ML models, and this course decides whether those projects produce trustworthy results.

Five years in

An engineer five years into practice writes small programs and sheets every week: a pump and pipeline sizing calculator, a machinery costing template for a custom-hiring centre, a script that cleans soil-moisture logger files and flags gaps and spikes, a database of tested equipment or of watershed structures with their locations and dates. They specify sensors and loggers for a project, choosing resolution, sampling interval and communication link for the site, and they write the calibration procedure. When a vendor offers an AI-based crop scouting or disease service, they ask how it was validated, on which crops and regions, and what its precision and recall are in field conditions, before recommending it to farmers.

Twenty-five years in

A senior engineer with 25 years of experience decides what information systems an organisation builds and trusts. They set data standards for a state irrigation or mechanisation programme: common identifiers, units, metadata and time conventions so data from different districts can be combined. They commission and judge decision support systems, insisting on validation against independent data, transparent assumptions and outputs that match the decisions farmers and managers actually make. They weigh build versus buy, interoperability against lock-in, and connectivity and power realities in rural areas. They also own data governance: consent, privacy, security of connected pumps and machines, and who may use farm data. Their main task is to stop impressive-looking dashboards from replacing sound engineering judgement.

PhD, twenty-five years in

A researcher with a PhD and 25 years of work uses computing as a scientific instrument. They design experiments and sensor networks that yield data suitable for modelling, couple process models of soil water, crop growth or drying with machine learning, and quantify uncertainty rather than reporting a single fitted number. Their methodological discipline shows in validation: spatially and temporally blocked cross-validation, independent test sites, comparison with simple baselines, and reporting of failures. They publish code and data with metadata under the FAIR principles so results can be reproduced, examine dataset bias in image-based detection, and train students to treat a high laboratory accuracy as a hypothesis to be tested in farmers' fields rather than a result.

Written from

This manual covers the eight course units at first-year B.Tech depth. It does not teach a full programming course: pointers beyond parameter passing, structures and dynamic memory in C, object-oriented programming, and Python libraries are introduced only in passing, and students must practise at a computer. Numerical methods are limited to root finding, integration, differentiation, Euler and Runge–Kutta, Gauss elimination and least squares; finite-element and finite-difference modelling are left to later courses. The ML chapters explain the main algorithms and evaluation in words and formulas without derivations of backpropagation, kernels or optimisation theory, and do not cover large language models, time-series forecasting or geostatistics. No named software, platform, device or service is described, and no communication technology's range or battery life is quoted as a number, because these vary with terrain, hardware and regulations. Data-protection law is mentioned only in general terms, because it differs by country; consult the law where you work. Specific crop-disease model accuracies are not quoted, because they depend on the dataset and test conditions.

Original practice questions written to the syllabus and pattern of the exams named in each question's examTags. They are not past papers and do not reproduce any board's questions. Every code fragment was run and every numerical answer computed by script before it was written; questions with no exam tag are written to professional practice rather than to a named exam.

Practise it, and prove it

These notes are free to read here, with no account.

What the Laisha app adds:

Free for ten days, then a subscription. We would rather write that here than have you find it later.

Open Laisha →