
- Forum
- Programming Talk
- C and C++
- Program related to arrays
Program related to arrays
This is a discussion on Program related to arrays within the C and C++ forums, part of the Programming Talk category; Suppose X takes values between -10 and 10 and Y = 10*sin (X*18). Plz help me to construct a c ...
-
Program related to arrays
Suppose X takes values between -10 and 10 and Y = 10*sin (X*18).
Plz help me to construct a c code for following
Accept a value of x from user. Suppose value of x=-5 then using the above eqn Y is calculated starting for x=-5, -4, -3, -2.......0,1,2.....9, 10, -10, -9, -8, -7, -6
The value of each Y is stored in array. i.e 1st element of array should contain value of x=-5 and so on.
similarly if user enters x=3 then Y shud b calculated starting from x=3, 4...10, -10, -9...-5, -2, -1, 0, 1 ,2 and 1st element of array shud contain value of y for x=3 and so on
-
hi there...you may need to include math.h in your program for using the sin function...
here's what you may be looking for...please check n let me know how it went out for u...
HTH!!!Code:#include <stdio.h> #include <math.h> int main(void) { int x; float y[21]; //for storing result for x...10 negative 10 positive and 0 equals 21 elements scanf("%d", &x); //ask user input for x, assuming it will always be valid (-10 to 10) for(int i=0; i < 21; i++) { y[i] = 10 * sin(x*18); //your formula printf("\n arrayindex = %d \t x = %d \t y = %f", i, x, y[i]); //lets print the values for i, x and y x++; // increment x if(11 == x) //have we crossed the boundary? { x = -10; //lets go back to -10 } } return 0; }
-
Please help me to solve one Array program:
Write a program that will input a 3x3 array of integers and determine if it is a "magic square." Magic squares have a special property: the values in each row, in each column, and in each diagonal add up get the same number.
Hints:
Declare variables as 3 by 3 matrix
Ask for and retrieve matrix values one row at a time as "Enter a 3x3 matrix below one row per line. Ex: 1 2 3"
Calculate the sum of each row
Calculate the sum of each column
Calculate the sum of diagonal
if all the sums equal each other, a magic square was entered which mean row1=row2=row3=column1=column2=column3=diagonal
Display "You input a magic square!!!"
Otherwise "This is not a magic square."
Thank in advances
-
hi esunly...I would definitely like to help you on this array program. But, giving the code directly won't help you in learning the program. I shall first give you hints on how you can go about doing it. Please put in your efforts in understanding the pseudo-code and put them in c syntax.
declare arr(3, 3)
declare row1sum, row2sum, row3sum, diagsum, col1sum, col2sum, col3sum
ask user input for row 1 and assign the values to arr(0, 0) arr(0, 1) arr(0, 2) respectively
ask user input for row 2 and assign the values to arr(1, 0) arr(1, 1) arr(1, 2) respectively
ask user input for row 3 and assign the values to arr(2, 0) arr(2, 1) arr(2, 2) respectively
row1sum = arr(0, 0) + arr(0, 1) + arr(0, 2)
row2sum = arr(1, 0) + arr(1, 1) + arr(1, 2)
row3sum = arr(2, 0) + arr(2, 1) + arr(2, 2)
col1sum = arr(0, 0) + arr(1, 0) + arr(2, 0)
col2sum = arr(0, 1) + arr(1, 1) + arr(2, 1)
col3sum = arr(0, 2) + arr(1, 2) + arr(2, 2)
diagsum = arr(0, 0) + arr(1, 1) + arr(2, 2)
if( row1sum equals row2sum equals row3sum equals col1sum equals col2sum equals col3sum equals diagsum)
output "You input a magic square!!!"
else
output "This is not a magic square!!!"
end if
You may find all the syntax you would need to code this routine in C on the following links,
C Programming - Arrays
C Programming - Managing Input and Output Operations
C Programming - Decision Making - Looping
Try this out...If you face any problems, paste in the code here and we shall guide you how to rectify it...
-
Sponsored Ads

Reply With Quote





