Hello everybody,can someone help me? i have to make a program who ilustrates Kirchhoff's first law.Thank You!

Dani AI

Generated

Good starting point, . If by "illustrate" you mean a quick console program that checks Kirchhoff’s Current Law (KCL) at a single node, keep it simple: read the signed currents for each branch, sum them, and report whether the total is (approximately) zero. That shows the rule numerically without needing any graphics. If you later need a diagram or multi-node solver, you can build on this.

Here is a minimal, standard C++ example. Use doubles (currents are rarely integers) and a small tolerance because of floating-point rounding. Positive means current into the node; negative means out of the node. It can also solve for one unknown branch if you have it.

#include <iostream>
#include <vector>
#include <numeric>
#include <cmath>

int main() {
    std::size_t n;
    std::cout << "Number of branches: ";
    if (!(std::cin >> n) || n == 0) return 0;

    std::size_t unknown = n; // set to n if none
    std::cout << "Index of unknown current (0.." << (n ? n-1 : 0) << ", or " << n << " if none): ";
    std::cin >> unknown;

    std::vector<double> I(n, 0.0);
    for (std::size_t k = 0; k < n; ++k) {
        if (k == unknown) continue;
        std::cout << "I[" << k << "] in amperes (+ into node, - out): ";
        std::cin >> I[k];
    }

    double sum_known = std::accumulate(I.begin(), I.end(), 0.0);
    if (unknown < n) {
        I[unknown] = -sum_known; // enforce KCL
        std::cout << "Solved I[" << unknown << "] = " << I[unknown] << " A\n";
        sum_known += I[unknown];
    }

    const double eps = 1e-9;
    std::cout << "Sum = " << sum_known << " A -> "
              << (std::fabs(sum_known) <= eps ? "KCL satisfied" : "KCL NOT satisfied")
              << "\n";
    return 0;
}

Notes that address your attempt and the follow-ups from and :

  • Use #include <iostream> (not iostream.h) and int main(), with std::cout/std::cin.
  • Avoid hard-coding -i1 + i2 - i3 + i4 = 0. Let the user enter any number of branches and directions.
  • Compile with something like: g++ -std=c++11 kcl.cpp -o kcl. Run it, then share the actual output if it still misbehaves.

Recommended Answers

All 3 Replies

First off, could you give more detail as to what you need? As I understand it, Kirchhoff's First Law is an equation regarding electrical flow across circuit intersections; Wikipedia gives the informal form of it as:

At any node (junction) in an electrical circuit, the sum of currents flowing into that node is equal to the sum of currents flowing out of that node, or: The algebraic sum of currents in a network of conductors meeting at a point is zero.

When you say you need the program to 'illustrate' Kirchhoff's First Law, do you mean it should calculate the values coming into and out of the node, from and to the given circuits; or that you need to simulate the circuits visually, as a circuit diagram (which would require the former computations, as well as the graphics handling)? Do you need to simulate a specific circuit that can be hard-coded into the program, or (more likely) does it need to be able to take a set of circuit definitions and translate those into the combined circuit?

Also, could you show us what work you've already done on the project? One of the rules of DaniWeb is that you need to show your effort on the project, in part so that we know where you are in it and don't duplicate work you've alrady done, and partly so we can tell that you have put a good-faith effort into the project yourself before asking for help. What code have you written for this so far, if any?

include<iostream.h>
include< match.h>

void main ()

int n,i,,S;
cout<<”n= “; cin>>n;
-i1+i2-i3+i4=0
This is what i write so far

What is is the output on the screen when you compile and run that code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.