Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I don't know what to do, to be honest (arrays and input loops in C)

0 votes
asked Nov 10, 2020 by Miafina (900 points)
edited Nov 10, 2020 by Miafina

Hello! I'm set to solve the problem but I understand nothing about it.

Function: ukl=(e^-ak)(ak+lbl), k=1,2,3, l=1,2,3,4.

Index designation: k.

Index number:   1 2

                        3 1 2

                        3 4

Value of index variable: 1.3 -

                                       1.6 -

                                        2.3

                                       4.51 -

                                       4.16

                                      2.61

                                     7.28

                                       

This is how I started solving the problem:

#include <stdio.h>
#include <math.h>
#define E 2,71828
int main()
{
    int k[3]={1,2,3};
    int l[4]={1,2,3,4};
    for(l=1;l<4;l++)
    {
        for(k=1;k<5;k++){
            
        }
    }
    return 0;
}

I'll be glad to get any help from you. Thank you in advance!

2 Answers

+1 vote
answered Nov 10, 2020 by Peter Minarik (19,180 points)
edited Nov 10, 2020 by Peter Minarik

Clear the requirements

  1. First of all, the formula is not clear at all.
    1. What is ukl? Is it a variable, or is it the product of three variables (u, k, l) i.e. i * k * l?
    2. Same applies for ak and lbl.
  2. What is an "index number"? What is it used for? Is it represented by any variable in the formula? Do you have one "index number" or multiple?

Clear the code

The code has problems too.

#define E 2,71828

Euler's number (e) is 2.71828. The decimal sign is dot, not coma for the programming languages.

#define e 2.71828

Looping through array elements

int l[4] = { 1, 2, 3, 4 };
for (l = 1; l < 4; l++)
{
    // ...
}

The code you provided will not work as you have defined the variable l as an array (int l[4];) but later you try to use it as a scalar, a simple integral number in the for loop.

To iterate through the elements of an array, you need to have an array indexer point to each of these elements one by one. In the blow example the indexer is i. We can access the ith element of the array by the l[i] expression.

const int l_count = 4;
int l[l_count] = { 1, 2, 3, 4 };
for (int i = 0; i < l_count; i++)
{
    printf("%d\n", l[i]);
    // ...
}

Please, note that the above code requires C++ to compile. If you want to use a C compiler, you have to use

#define l_count 4

instead.

One more important aspect of the code is that (in most programming languages) indexing arrays starts from 0. If you have n elements in the array, then the greatest index is n - 1. Hence the for loop starts from 0 and end if the index variable (i) is no longer less than the count of elements in l (l_count).

I hope this helps.

commented Nov 10, 2020 by Miafina (900 points)
No, I have cleared the formula I have to calculate. k and I are indexes, I and b are arguments. Thank you, Peter! But still I don't undesstand these indexer numbers and values of index variables(((
commented Nov 11, 2020 by Miafina (900 points)
I think I 'm starting to understand this task. So this is my program using for:
#include <stdio.h>
#include <math.h>
#define E 2.71828
int main()
{
    float u;
    float A[3]={1.3,1.6,4.51};
    float B[4]={2.3,4.16,2.61,7.28};
    //printf("Заданий масив A\n");
    for(int k=0;k<3;k++){
         printf("A=%.2f\n",A[k]);   
        }
    //printf("\nЗаданий масив B\n");
    for(int l=0;l<4;l++)
    {
        printf("B=%.2f\n",B[l]);
        for(int k=0;k<3;k++){
          u=pow(E,-A[k])*(A[k]+l*B[l]);
          printf("U=%.3f\n", u);
          }
    }
    return 0;
}
The results:A=1.30                                                                                                                         
A=1.60                                                                                                                         
A=4.51                                                                                                                         
B=2.30                                                                                                                         
U=0.354                                                                                                                         
U=0.323                                                                                                                         
U=0.050                                                                                                                         
B=4.16                                                                                                                         
U=1.488                                                                                                                         
U=1.163                                                                                                                         
U=0.095                                                                                                                         
B=2.61                                                                                                                         
U=1.777                                                                                                                         
U=1.377                                                                                                                         
U=0.107                                                                                                                         
B=7.28                                                                                                                         
U=6.306                                                                                                                         
U=4.732                                                                                                                         
U=0.290      
And this is my code using do while:
#include <stdio.h>
#include <math.h>
#define E 2.71828
int main()
{
    float u; int l=0;
    float A[3]={1.3,1.6,4.51};
    float B[4]={2.3,4.16,2.61,7.28};
    //printf("Заданий масив A\n");
    for(int k=0;k<3;k++){
         printf("A=%.2f\n\t",A[k]);   
        }
    //printf("\nЗаданий масив B\n");
    do
    {
        printf("B=%.2f\n\t",B[l]);
        for(int k=0;k<3;k++){
          u=pow(E,-A[k])*(A[k]+l*B[l]);
          printf("U=%.3f\n\t", u);
          }
          l++;
    }
    while(l<4);
    return 0;
}
Results:A=1.30                                                                                                                         
        A=1.60                                                                                                                 
        A=4.51                                                                                                                 
        B=2.30                                                                                                                 
        U=0.354                                                                                                                 
        U=0.323                                                                                                                 
        U=0.050                                                                                                                 
        B=4.16                                                                                                                 
        U=1.488                                                                                                                 
        U=1.163                                                                                                                 
        U=0.095                                                                                                                 
        B=2.61                                                                                                                 
        U=1.777                                                                                                                 
        U=1.377                                                                                                                 
        U=0.107                                                                                                                 
        B=7.28                                                                                                                 
        U=6.306                                                                                                                 
        U=4.732                                                                                                                 
        U=0.290    
I hope I've done everything right. But now I have to make the results look more organised, but I don't know how!
Thank you again, Peter!
commented Nov 11, 2020 by Peter Minarik (19,180 points)
Hi.

Now that you made your formula look more friendly (using lower indices) I can confirm that your code { u=pow(E,-A[k])*(A[k]+l*B[l]) } does the same thing as you wanted to do in your formula.

How to make the results more organised? Well, you can put everything in a table. Or a graph (look up Matlab, if you haven't heard of it yet).

You can make simple tables by printing on the standard output (or in a file, which can be also achieved by redirecting the standard output. e.g. MyProgram.exe > MyOutput.txt).
+1 vote
answered Nov 11, 2020 by Peter Minarik (19,180 points)

I've added some easy formatting for your program. Please, see below:

#include <stdio.h>
#include <math.h>

#define E 2.71828

#define ArrayLength(array) (sizeof(array) / sizeof((array)[0]))

static void PrintArray(const char * name, const float * array, size_t count)
{
    printf("%s: { ", name);
    for (size_t i = 0; i < count; i++)
    {
        if (i != 0)
            printf(", ");
        
        printf("%.3f", array[i]);
    }
    printf(" }\n");
}

static void PrintInput(const float * A, size_t countA, const float * B, size_t countB)
{
    PrintArray("A", A, countA);
    PrintArray("B", B, countB);
    printf("\n");
}

static void PrintFormula()
{
    printf("u = pow(E, -A[k]) * (A[k] + l * B[l])\n");
    printf("\n");
}

static void PrintHeader()
{
    printf(" k | l |   u\n");
    printf("---------------\n");
}

static void PrintRecord(int k, int l, float u)
{
    printf(" %d | %d | %.3f\n", k, l, u);
}

int main()
{
    float A[3] = { 1.3, 1.6, 4.51 };
    float B[4] = { 2.3, 4.16, 2.61, 7.28 };

    PrintInput(A, ArrayLength(A), B, ArrayLength(B));
    PrintFormula();
    PrintHeader();
    
    for (int l = 0; l < 4; l++)
    {
        for (int k = 0; k < 3; k++)
        {
            PrintRecord(k, l, pow(E, -A[k]) * (A[k] + l * B[l]));
        }
    }
    
   return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...