here is my code, as you can see I used arrays of structures. The question is how should I take the arrays element by user directly? You can see below how the elements are given to the program before execution.
any help will be appericiated.
#include<iostream>
#include<math.h>
using namespace std;
struct Data
{
int x, y;
};
double interpolate(Data f[], int xi, int n)
{
double P = 0;
for (int i = 0; i<n; i++)
{
double p= f[i].y;
for (int j = 0;j<n;j++)
{
if (j != i)
p =p*(xi - f[j].x) / (f[i].x - f[j].x);
}
P += p;
}
return P;
}
int main()
{
Data f[] = { { 0,2 },{ 1,3 },{ 2,12 },{ 5,147 }};
cout << "Value of f(3) is : " << interpolate(S, 3, 4) << endl;
system("pause");
return 0;
}
#include<iostream>
#include<math.h>
using namespace std;
struct Data
{
int x, y;
};
double interpolate(Data f[], int xi, int n)
{
double P = 0;
for (int i = 0; i<n; i++)
{
double p= f[i].y;
for (int j = 0;j<n;j++)
{
if (j != i)
p =p*(xi - f[j].x) / (f[i].x - f[j].x);
}
P += p;
}
return P;
}
int main()
{
int input[8];
cout << "Enter first value: \n";
cin >> input[0];
cout << "Enter second value: \n";
cin >> input[1];
cout << "Enter third value: \n";
cin >> input[2];
cout << "Enter fourth value: \n";
cin >> input[3];
cout << "Enter fifth value: \n";
cin >> input[4];
cout << "Enter sixth value: \n";
cin >> input[5];
cout << "Enter seventh value: \n";
cin >> input[6];
cout << "Enter eighth value: \n";
cin >> input[7];
Data f[] = {{input[0], input[1]}, {input[2], input[3]}, {input[4], input[5]}, {input[6], input[7]}};
cout << "Value of f(3) is : " << interpolate(f, 3, 4) << endl;
system("pause");
return 0;
}
Related
The goal is to search the array and find a specific number in it and return the position. I have the method for how to do it down but trying to get it to run in the main method is giving me the error in the title. What do i do to fix?
#include "stdafx.h"
#include <iostream>
using namespace std;
int searchArray(int a[], int x) {
for (int i = 0; i < a[100]; i++) {
if (x == a[i])
return i + 1;
break;
}
return -1;
}
int main()
{
int wait, x, y, a[100];
//problem 3
cout << "Enter the size of the array(1-100): ";
cin >> y;
for (int i = 0; i < y; i++) {
cout << "Enter an array of numbers:";
cin >> a[i];
}
searchArray(a[100], x); //i get error on this line with a[100]
cin >> wait;
return 0;
}
Expected is it should run with no errors and find position of a number in the array but I just get the error and cant run it.
After the for() loop in main(), you need something like:
cout << "Enter the value to search for: ";
cin >> x;
wait = searchArray(a, x);
cout << x;
cout << " is at position: ";
cout << wait;
int searchArray(int a[], int x)
{
for (int i = 0; i < 100; i++) //change a[100] to 100 because it only needs the size not the array itself
{
if (x == a[i])
return i + 1;
break;
}
return -1;
}
int main()
{
int wait, x, y, a[100];
cout << "Enter the size of the array(1-100): ";
cin >> y;
for (int i = 0; i < y; i++) {
cout << "Enter an array of numbers:";
cin >> a[i];
}
cout<<"Number to look for: "; //sets a value for x
cin>>x;
cout<<"Index: "<<searchArray(a, x)<<endl; //function returns an int therefore a cout is needed
cin >> wait;
return 0;
}
The program should read n resistances and a voltage from the keyboard and then calculate the equivalent resistance and the current.
My problem is that it calculates based only on the last entered resistance.
Is it possible to declare a method inside a function? or should I give up this completely unpractical approach
#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
class rez {
float r;
public:
void set(int n);
float val() { return r; }
};
void rez :: set(int n) { //n is the number of resistances
int i;
for (i = 1; i <= n; i++) {
cout << "R" << i << "=";
cin >> r;
}
}
float serie(rez r1,int n)
{
float s=0;
int i;
for (i = 1; i <= n; i++)
{
s = s+ r1.val();
}
return s;
}
float para(rez r1, int n)
{
float s = 0;
int i;
for (i = 1; i <= n; i++)
{
s = s + (1/r1.val());
}
return 1/s;
}
int main()
{
char c, k = 'y'; // 'c' selects series or para
rez r1;
int n;
cout << "number of resis:";
cin >> n;
cout << endl;
while (k != 'q')
{
r1.set(n);
float i, u;
cout << "\n Vdc= ";
cin >> u;
cout << endl;
cout << "series or para(s/p)?"<<endl;
cin >> c;
switch (c)
{
case('s'):cout <<"\n equiv resistance = "<< serie(r1,n)<<endl;
i = u / serie(r1, n);
cout << "curr i = " << i << " amp";
break;
case('p'):cout << "\n equiv res = " << para(r1, n)<<endl;
i = u / para(r1, n);
cout << "cur i = " << i << " amp";
break;
}
cout <<endl<< "\n another set?(y/q)?"<<endl;
cin >> k;
}
return 0;
}
It is because when you read in the resistances you are setting the value of the total resistance each time not adding to the total resistance.
void rez :: set(int n) { //n is the number of resistances
int i;
for (i = 1; i <= n; i++) {
cout << "R" << i << "=";
cin >> r; // <- this sets the value of r, it does not add to it
}
}
To fix this you should create a temporary variable to store the input resistance and then add it to the total resistance
void rez :: set(int n)
{
int i;
for (i = 1; i <= n; i++)
{
float input;
cout << "R" << i << "=";
cin >> input;
r += input;
}
}
i have to write a program which read from the keyboard a line of numbers and save them into an array, numbers have to be written just in a line, i wrote this but doesn`t work because of an infinite loop, any suggestion?
int main() {
int numCasos = 0, contNumCasos = 0, numElem = 0;
string aux;
cout << "Number of cases: " << endl;
cin >> numCasos;
while (contNumCasos < numCasos) {
cout << "Number of elements: " << endl;
cin >> numElem;
cout << "Enter the Elements separated by space: " << endl;
cin.ignore();
vector.cont = 0;
int i = 0;
while ((vector.cont < numElem) && getline(cin,aux,' ')){
vector.v[i] = stoi(aux);
vector.cont++;
i++;
}
}
cout << sumaBuenos(vector) << endl;
cin.ignore();
system("pause");
return 0;
}
An example:
console: Number of elements:
user: 4
console: Enter the Elements separated by space:
user: 2 43 65 56
--this has to be the vector
-- vector.v[0] = 2
-- vector.v[1] = 43
-- vector.v[2] = 65
-- vector.v[3] = 56
if you know how many numbers you have to read there is a simpler way:
int n;
cin>>n; // how many numbers;
vector<int> v(n);
for ( int i=0; i<n; ++i ){
cin>>v[i];
}
With a modification it works.
int main() {
int numCasos = 0, contNumCasos = 0, numElem = 0;
string aux;
cout << "Numero de casos: " << endl;
cin >> numCasos;
while (contNumCasos < numCasos) {
cout << "Numero de elementos: " << endl;
cin >> numElem;
cout << "Ingrese los elementos separados por espacios: " << endl;
cin.ignore();
vector.cont = 0;
getline(cin, aux);
istringstream iss(aux);
for (int i = 0;i < numElem;i++) {
iss >> aux;
vector.v[i] = stoi(aux);
vector.cont++;
}
cout << sumaBuenos(vector) << endl;
contNumCasos++;
}
system("pause");
return 0;
}
to someone who have the same problem.
#include<iostream>
using namespace std;
int main()
{
int n;
cout << "input numbers" << endl;
cin >> n;
vector<int> v(n);
for ( int i=0; i<n; ++i )
{
cin >>v[i];
}
return 0;
}
int main()
{
cout << "The maximum number of elements in an array is 100. " << endl;
const int MAX = 100;
double myarray[MAX];
int arraysize = Fill_Array(myarray, MAX);
Show_Array(myarray, arraysize);
Reverse_Array(myarray, arraysize);
Show_Array(myarray, arraysize);
Reverse_Positions(myarray, arraysize);
Show_Array(myarray, arraysize);
return 0;
}
int Fill_Array(double ar[], int size)
{
int i = 0;
cout << "\nEnter a double value (Enter q to quit): ";
while (cin >> ar[i] && i < size)
{
i++;
cout << "Enter next value: ";
}
return i;
}
void Show_Array(const double ar[], int size)
{
cout << "\nHere is your array: " << endl << endl;
for (int i = 0; i < size; ++i)
cout <<"\#"<<i+1<<" is "<<ar[i] << endl;
}
void Reverse_Array(double ar[], int size)
{
cout << "Now we reverse the whole array: " << endl;
for (int i = 0; i != size / 2; ++i)
{
double temp;
temp = ar[i];
ar[i] = ar[size - 1 - i];
ar[size - 1 - i] = temp;
}
}
void Reverse_Positions(double ar[], int size)
{
cout << "Enter two positions: " << endl;
int a, b;
if (cin >> a >> b)
{
double temp = ar[b - 1];
ar[b - 1] = ar[a - 1];
ar[a - 1] = temp;
}
}
Something wrong with reverse_position function: It only shows "Enter two positions" and then the function terminates. There is no chance for me to enter the integers a and b. I doubt there is something wrong with the input queue, but I am not sure.
Basically, I'm trying to set the size of my array according to an input (numArraySize will be asked for from the user). I created an array pointer in main and want to pass this pointer to the functions.
#include <iostream>
using namespace std;
// Algorithm for Insertion Sort
int numArraySize;
void inputNums(int numArray[])
{
cout << "Enter a bunch of numbers: " ;
for(int x=0; x<numArraySize; x++)
{
cin >> numArray[x];
}
}
void outputNums(int numArray[])
{
for(int x=0; x<numArraySize; x++)
{
cout << numArray[x];
if(x != numArraySize-1)
{
cout << " - ";
}
}
}
void insertionSort(int numArray[])
{
int num;
int i;
for(int j=1; j<numArraySize; j++)
{
num = numArray[j];
i = j-1;
while(i>=0 && numArray[i] > num)
{
numArray[i+1] = numArray[i];
i--;
}
numArray[i+1] = num;
}
}
int main()
{
int *numbers = new int[numArraySize];
int choice;
cout << "1) Insertion Sort" << endl;
cout << "Enter your choice: " << endl;
cin >> choice; // Input choice for which algorithm to use
if(choice == 1)
{
cout << "Enter size of the array: ";
cin >> numArraySize;
inputNums(numbers); // Insert numbers
insertionSort(numbers); // Use algorithm to sort then output
outputNums(numbers);
}
cout << endl;
return 0;
}
EDIT:
Ok i fixed the error. I changed:
"int numArraySize=1;"
and the position of
"int *numbers = new int[numArraySize];"
The problem was that i had to initialize the numArraySize anyways. That causes error apparently. And the second problem was i have to input numArraySize before i initialize
"int *numbers" in the main function.
#include <iostream>
using namespace std;
// Algorithm for Insertion Sort
int numArraySize=1;
void inputNums(int numArray[])
{
cout << "Enter a bunch of numbers: " ;
for(int x=0; x<numArraySize; x++)
{
cin >> numArray[x];
}
}
void outputNums(int numArray[])
{
for(int x=0; x<numArraySize; x++)
{
cout << numArray[x];
if(x != numArraySize-1)
{
cout << " - ";
}
}
}
void insertionSort(int numArray[])
{
int num;
int i;
for(int j=1; j<numArraySize; j++)
{
num = numArray[j];
i = j-1;
while(i>=0 && numArray[i] > num)
{
numArray[i+1] = numArray[i];
i--;
}
numArray[i+1] = num;
}
}
int main()
{
int choice;
cout << "1) Insertion Sort" << endl;
cout << "Enter your choice: " << endl;
cin >> choice; // Input choice for which algorithm to use
if(choice == 1)
{
cout << "Enter size of the array: ";
cin >> numArraySize;
int *numbers = new int[numArraySize];
inputNums(numbers); // Insert numbers
insertionSort(numbers); // Use algorithm to sort then output
outputNums(numbers);
}
cout << endl;
system("pause");
return 0;
}
Thanks for your helps :)
To pass an array to a function, you can simply use int *array:
void outputNums(int *numArray)
{
for(int x=0; x<numArraySize; x++)
{
cout << numArray[x];
if(x != numArraySize-1)
{
cout << " - ";
}
}
}
Please note that it would be better to use a standard container like a vector.
When you are allocating the memory for the array in line
int *numbers = new int[numArraySize]
, you still haven't received the desired size for the array in variable numArraySize.