Question

I am trying to make a program to print maximum of 5 numbers using for loop and taking number input from user.

I know I can do it via max command by having a =[1,2,3,4,5]; and max(a);. But trying out with for loop.

I don't know how to take an array in Scilab (I know their is matrix that we can take but don't know how to take input from user in matrix or in array in Scilab)

`a = [1,2,3,4,5];` //works fine but i want user should input numbers.

I know one way is using

a = input("First number:");
 b = input("Second number:"); ... and so on upto fifth number 
// i want to make it short like using array in C language
int a[5];
printf("Enter numbers");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
// Here in Scilab i don't know how i write it??
if I use int a[5]; i get error Undefined variable: a --error4

I know i can use mprintf & msscanf but question is i am not able to declare or take array data from user's end. Please suggest me some way to make this program.

Also how to declare & take matrix data from user and declare & take array data from user.

Était-ce utile?

La solution

Comments on your solution

Entering matrices

I think your solution is valid, however it could be frustrating for the user to keep providing entries if you have a lot of entries and you know you mistyped the first.

A nice function to have a look at is x_matrix. It provides an easy interface for editing matrices.

Functions

I also usually really prefer functions, it makes it much easier to reuse your code and validate and test small portions. Naturally in this problem the SciLab provided function max() should be used, but you stated you wanted to use a for loop.

Code example

Taking into account the above statements, here is a small working example. You could expand it to let the user first provide the matrix dimensions.

function maximum = findMax( numbers )

    maximum = -1e99;

    numberOfNumbers = length( numbers );

    for i=1:numberOfNumbers
        if( numbers(i) > maximum )
            maximum = numbers(i);
        end
    end

    return maximum;

endfunction

[result]=x_matrix("enter a matrix", zeros(5,5) );

foundMaximum = findMax(result);
disp( "Maximum is " + string( foundMaximum ) );

Autres conseils

I managed to write this code it work's fine now. But i want more answers also to learn more ways.

disp("Enter Numbers:"); // Enter first number on console then press enter key then type second number and again press enter to type third ..... so on to fifth.
    for i = 1:5
        x(i) = input('');
    end
    maximum = x(1)
    for i=1:5
        if(x(i)>maximum)then
            maximum = x(i)
        end
    end
    disp(maximum, "Maximum Number is");

Is it the correct way to write this program and taking input from user this way ?? Qestions remains How to declare & take matrix data from user and declare & take array data from user. More Answers Needed.

I had a similar problem and found this nice way from the Scilab Help:

labels=["magnitude";"frequency";"phase    "];
[ok,mag,freq,ph]=getvalue("define sine signal",labels,...
     list("vec",1,"vec",1,"vec",1),["0.85";"10^2";"%pi/3"])

I liked it because you have labels and get a good overview over your data.

In the example above no for loop is included but lists can be created quite flexible and you can for example create the list in a for loop if you need that kind of pattern.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top