So I was trying to work on a code about Jordan's elimination. When I encountered a problem:
Cannot convert 'float' to 'float ()[100]' for argument '1' to 'int diabase(float ()[100])
What does this mean? And how can I fix it?
#include<stdio.h>
#define N 100
int read(float A[N][N]);
int jordan(float A[N][N],int n);
int print(float A[N][N],int n);
int main()
{
int i, j, k, n, y;
float A[N][N+1], c, x[N];
n = read(A[N][N]);
jordan(A[N][N]], n);
print(A[N][N], n);
return(0);
}
int read(float A[N][N]){
int n,i,j;
printf("Enter the size of matrix: ");
scanf("%d",&n);
printf("Enter the elements of augmented matrix row-wise:");
for(i=1; i<=n; i++)
{
for(j=1; j<=(n+1); j++)
{
printf(" A[%d][%d]:", i,j);
scanf("%f",&A[i][j]);
}
}
return n;
}
int print( float A[N][N],int n){
int i;
float x[n];
printf("The solution is:");
for(i=1; i<=n; i++)
{
x[i]=A[i][n+1]/A[i][i];
printf("n x%d=%fn",i,x[i]);
}
return 0;
}
int jordan(float A[N][N],int n){
int i,j,k;
float c;
for(j=1; j<=n; j++)
{
for(i=1; i<=n; i++)
{
if(i!=j)
{
c=A[i][j]/A[j][j];
for(k=1; k<=n+1; k++)
{
A[i][k]=A[i][k]-c*A[j][k];
}
}
}
}
return 0;
}
In all of these function calls
n=read(A[N][N]);
jordan(A[N][N]],n);
print(A[N][N],n);
You are passing and element of the array instead of the array. If you want to pass the array you just use the name of the array without any indexes
n=read(A);
jordan(A,n);
print(A,n);
You are going to run into another problem with this though as your functions expect a 2d array of 100 x 100 but your array you created in main() is 100 x 101 which is not going to match. You either need to make the array in main() 100 x 100 or change you function take arrays of 100 x 101
When you do function (A[N][N]) you are passing a single float value to function, so here:
n = read(A[N][N]);
jordan(A[N][N], n);
print(A[N][N], n);
You are passing single values to read, jordan and print. And B.T.W you are accessing cells that do not exist (A[N] is out of bound since A is float [N][N + 1].
If you want to pass A to your function, simply do:
n = read(A);
jordan(A, n);
print(A, n);
Apart from that, there are some mistakes in your code:
Your functions want a float [N][N] but A is float [N][N+1] so you will have to modify something.
You are accessing cells from 1 to n/n+1, C arrays have cells from 0 to n-1/n, so your loop should go from 0 to n-1/n.
Your main should be
int main()
{
float A[N][N];
int n = read(A);
jordan(A, n);
print(A, n);
return(0);
}
Related
my problem i face is i cant run my code when i use cin>>arr
can i make it work with this code
#include <bits/stdc++.h>
using namespace std;
int checkEqualSumUtil(int arr[1000], int N,
int sm1, int sm2,
int sm3, int j)
{
if (j == N)
{
if (sm1 == sm2 && sm2 == sm3)
return 1;
else
return 0;
}
else
{
int l = checkEqualSumUtil(arr, N,
sm1 + arr[j],
sm2, sm3, j + 1);
int m = checkEqualSumUtil(arr, N, sm1,
sm2 + arr[j],
sm3, j + 1);
int r = checkEqualSumUtil(arr, N, sm1, sm2,
sm3 + arr[j], j + 1);
return max(max(l, m), r);
}
}
void checkEqualSum(int arr[], int N)
{
int sum1, sum2, sum3;
sum1 = sum2 = sum3 = 0;
if (checkEqualSumUtil(arr, N, sum1,
sum2, sum3, 0)== 1)
{
cout << "YES";
}
else
{
cout << "NO";
}
}
int main()
{
int n;
cin>>n;
int arr[n/2];
for(int i=0;i<n;i++){
cin>>arr[i];
int N = sizeof(arr) / sizeof(arr[0]);
checkEqualSum(arr, N);
return 0;
}}
my code
i have edited my code the input is typed correctly
but it always outputs no
i dont know why any ideas??
also thank you for helping me all
i tried the input
3
1 1 1
it should print yes
put the output is no?
Let's analyze your code in main:
int main()
{
int arr[1000],n;
You have allocated an array, arr, that has a capacity of 1000 integers.
The array is not initialized.
An alternative declaration:
const unsigned int N = 1000;
int array[N];
The statement below reads the quantity of numbers to read into the array.
cin>>n;
If the User or Operator enters a value larger than 1000, you will have undefined behavior. Quantities less than 1000 are a waste of space. Use std::vector<int> to contain elements when the capacity is not known during runtime.
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
Here you are calculating the capacity of the array. The capacity was already declared as 1000. See my suggestion about using N as the capacity.
int N = sizeof(arr) / sizeof(arr[0]);
The above line is not necessary. Another option is to use a macro:
#define MAXIMUM_NUMBERS (1000)
The present form of the statement below, tells the function to use 1000 values, regardless of the quantity of numbers entered by the User.
checkEqualSum(arr, N);
For example, if I enter 5 numbers, the function call will translate to:
checkEqualSum(arr, 1000);
This because you initialized N to the capacity of the array.
Maybe you want to pass the quantity of numbers read into the array:
checkEqualSum(arr, n);
This is why variable names should differ in more than case.
This code is fine, telling the Operating System that your code run with no errors.
return 0;
}
I'm begging for your help guys, I'm not the best coder, and I've spent so much time on this, I'm so tired and depressed :[
Basically I want to pass an int, and 4 single dimension arrays to a function that shall return a pointer to an array(solution vector for my purpose) which I understood in C I could define in a function through the order static.
Basically it says that it doesn't understand why a double type array is passed to a function that returns a pointer to double, and for some reason it expects a pointer to be the argument as well as I understood it.
and I think it dragged to the bolded line, because there's a problem of compatibility, at least I think thats the reason for that error.
Please help me guys :]
#include <stdio.h>
#define N 4
double* thomas_algorithm(int n, double c[], double b[], double a[], double
d[]);
int main()
{
int i, n;
double* p;
double a[N-1]={0}, b[N]={0}, c[N-1]={0}, d[N]={0};
printf("please enter the order of the coefficient matrix:\n");
scanf("%d", &n);
printf("please insert the vector c:\n");
for(i=0; i<N-1; i++)
{
scanf("%lf", &c[i]);
}
printf("please insert the vector b:\n");
for(i=0; i<N; i++)
{
scanf("%lf", &b[i]);
}
printf("please insert the vector a:\n");
for(i=0; i<N-1; i++)
{
scanf("%lf", &a[i]);
}
printf("please insert the vector d:\n");
for(i=0; i<N; i++)
{
scanf("%lf", &d[i]);
}
**p=thomas_algorithm(n, c[N-1], b[N], a[N-1], d[N]);**
for(i=0; i<N; i++)
{
printf("x(%d)=%f", i+1, p+i);
}
return 0;
}
double* thomas_algorithm(int n, double c[], double b[], double a[], double
d[]) {
int i;
static double x[N]={0};
for(i=1; i<n-1; i++) /*factorization phase*/
{
b[i]=b[i]-(a[i]/b[i-1])*c[i-1];
d[i]=d[i]-(a[i]/b[i-1])*d[i-1];
}
/*backward substitution*/
x[N-1]=d[N-1]/b[N-1];
for(i=n-2; i>-1; i++)
{
x[i]=(d[i]-c[i]*x[i+1])/b[i];
}
return x;
}
Your function call is wrong.
if you have an array int a[N]; and a function void func(int a[]) you need to call the function like func(a);.
In your call you pass the Nth element of the array a[N], therefore the compile error since it is of type double and not double *. (It is also an out of bounds access)
The correct function call would be:
p=thomas_algorithm(n, c, b, a, d);
I am having difficulties with a program that does not execute a function and i cant seem to find the problem.
This little piece of code that I'm trying to write should ask the user to enter the size of the 2d array and after that search each row and calculate the average of the rows.
It executes just fine until the calculation results come up.
The example:
Enter the size of the array: 2
2
Enter the element of the 1 row and 1 column: 10
Enter the element of the 1 row and 2 column: 20
Enter the element of the 2 row and 1 column: 50
Enter the element of the 2 row and 2 column: 20
Program ended with exit code: 0
and code of the program:
#include <iostream>
#include <iomanip>
using namespace std;
void calculate(int n, int m, int matrix[10][10], double sum, double avg[10], int k); //to calculate the average of each row
void input(int n, int m, int matrix[10][10]); //to input the requirements
void results(double avg[10],int n); //to output the results
int main() {
int matrix[10][10]; //the array
int n,m; //rows and columns entered by the user
double avg[10]; //average of the array rows, which will be calculated later
int k; //number of positive elements
double sum; //to calculate sum
input(n, m, matrix);
calculate(n, m, matrix, sum, avg, k);
results(avg, n);
return 0;
}
void input(int n, int m, int matrix[10][10]) {
cout<<"Enter the size of the array: ";
cin>>n>>m; //the real elements of the array
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
cout<<"Enter the element of the "<<i+1<<" row and "<<j+1<<" column: "; //entering each element of the array
cin>>matrix[i][j];
}
}
}
void calculate(int n, int m, int matrix[10][10], double sum, double avg[10], int k) {
for (int i=0; i<n; i++) {
k=0;
sum=0;
avg=0;
for (int j=0; j<m; j++) {
if (matrix[i][j]>0) {
sum+=static_cast<double>(matrix[i][j]);
k++;
}
}
if (k>0) {
avg[i]=sum/static_cast<double>(k);
}
}
}
void results(double avg[10], int n) {
for (int i=0; i<n; i++) { //
cout<<"Average of "<<i<<" row is equal to: "<<avg[i]<<"\n";
}
}
There are multiple problems with your code:
You ask the user for the size of your array, but you have the array defined with constant size:
int matrix[10][10];
What would happen if user entered 11? It would result in an undefined behavior. Consider using std::vector if you want to have truly dynamic arrays.
When you read your n, m values inside void input(int n, int m, int matrix[10][10]) procedure, you are making changes to the copies of those variables (i.e. they are passed by value), so, the changes are only visible to the inside of the function. When you leave the scope of that function, all changes you did to them, are lost. You need to pass those parameters by reference, i.e.:
void input(int& n, int& m, int matrix[10][10]);
That way, the compiler won't do a copy, and you will be changing the same variables from your main.
Having that in mind, you would need to change your calculate procedure in a similar way:
void calculate(int n, int m, int matrix[10][10], double& sum, double avg[10], int& k);
No need for variables n, and m to be passed by reference, since, in this case, they are input parameters, and don't need to be changed.
You are not changing n and m in main(). input() takes in the parameters by value which mean it makes a copy so the changes done in the function are local only to the function. To fix this just pass n and m by reference.
void input(int& n, int& m, int matrix[10][10])
I am trying to create a code that reads in an array, finds the smallest element in that array, and then subtracts that smallest element from all the other elements in the array and prints a new array with these elements.
The code I have written so far will not even compile.
Here is what I have:
#include <iostream>
#include <iomanip>
using namespace std;
const int SIZE(25);
void read_list(int a[], int&num_ele);
void print_array(const int a[], const int num_ele);
int find_min(const int a[], const int num_ele);
void array_subtract(int x, int&a[], int num_ele) ;
int main()
{
int num_ele(0);
int array[SIZE] ;
read_list(array, num_ele);
cout<<"Before list: ("<<num_ele<< " numbers): " <<endl;
print_array(array, num_ele) ;
min = find_min(array, num_ele) ;
cout<<"The minimum value = "<<min<<endl;
array_subtract(min ,array ,num_ele) ;
cout<<"After list: ("<<num_ele<< " numbers): " <<endl;
print_array(array, num_ele) ;
return 0;
}
void read_list(int a[], int&num_ele)
{
int x(0);
cout<<"Enter positive numbers (ints) terminated 0: "<<endl;
cin>>x;
while(x!=0 && num_ele <= SIZE)
{
a[num_ele] = x ;
num_ele++;
cin >> x;
}
}
void print_array(const int a[], const int num_ele)
{
for(int i=0; i<num_ele; i++)
{
if(i<num_ele-1)
{cout<<a[i]<<", "; }
else
{cout<<a[i]<<"."}
}
}
int find_min(const int a[], const int num_ele)
{
int x= a[0];
for(int k=0; k<num_ele; k++)
{
if(a[k] < x)
{ x=a[k] ;}
}
return x ;
}
void array_subtract(int x, int&a[], int num_ele)
{
for(int j=0; j<num_ele; j++)
{
a[j] = a[j]-x ;
}
}
When I go to compile, at the line
cout<<"The minimum value = "<<min<<endl;
I get about 100 lines of errors.
Is there some huge error in my code I am missing? Why would this happen? I would like to keep the code in the same format it is in (calling functions to get the final output).
Thanks
You have 3 problems, after correcting these problems, at least you have not compile errors.
i.
You're passing arrays wrong:
void array_subtract(int x, int&a[], int num_ele);
^^^^^^^
Pass it like below (remove &)
void array_subtract(int x, int a[], int num_ele);
ii.
You're using variable min without declaring it. Declare it like below:
int min;
iii.
You've missed a semicolon ; in function print_array:
cout<<a[i]<<".";
^
You did not define variable min so the compiler reports errors when encounters statement
min = find_min(array, num_ele) ;
Aside from the compilation errors addressed by others...
Instead of using an array, use a vector (since logically you array is variably sized from 0-SIZE, and a vector is a variable sized array)...
vector<int> v;
Accept integer input until 0 is typed...
for(int n; cin >> n && n != 0;)
v.push_back(n);
Find the min element...
auto min = min_element(begin(v), end(v));
Printing can be done a variety of ways...
cout << "Vector: [";
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << "]\n";
Subtract the min from the elements...
transform(v.begin(), v.end(), v.begin(), [min](int n){ return n-min; });
Try to use the C++ standard library when possible instead of hand rolling loops. It reduces the potential for bugs, and is often clearer (but not always... for example that print I posted above doesn't exactly read intuitively IMO)
So my program makes a random (x,y) arrays, what i want to do is to make x a real number and y imaginary number and add them:
my main program
#include "complx.h"
int main(){
const int n = 2;
Complex a[n];
getData(a,n);
printArray(a,n);
isort(a,n);
printArray(a,n);
complex_sum(a,n);
return 0;
}
it also prints and arranges the arrays, but what I am interested in is how to add the arrays, when for example I would use 4 arrays (x,y)(x,y)(x,y)(x,y).
this is how i get a random numbers
void getData(Complex a[],int n){
int i;
srand(time(0)); //If comment this out, get same sequence with each run.
for(i=0; i<n; i++){
a[i].x = rand()%3; //3 and 10 are just for testing isort
a[i].y = rand()%10;
}
return;
}
and here is how i'm trying to add it:
void complex_sum(Complex a[], int n){
for (int i=0; i<n; i++)
cout<<"("<<a[i].x+a[i].x<<")";
cout<<endl;
return;
I am stuck on how to add (x,y)(x,y)= x+yi
thanks
I am not completely inline with the way you are attempting; but to sum n number of complex numbers stored in an array and print it on screen you can try the following:
void complex_sum(Complex a[], int n){
int real=0,img=0;
for (int i=0; i<n; i++){
real+=a[i].x;
img+=a[i].y;
}
cout<<"("<<real << "+" << img << "i)";
}