The point of the program is to send data from 1 array to another array I'm not sure what's wrong with how I'm passing it. It should enter the data in 1 array then call upon the copy function and puts itself there and then the array is traversed.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
#include<math.h>
#include<ctype.h>
#include<stdbool.h>
double copy_arr(double source[n][u],double target[n][u],int n,int u);
int main(void)
{
double source[3][5]={{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5}};
double target1[3][5];
copy_arr(source,target1,3,5);
int j;
int i;
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
printf("%f 1",target1[i][j]);
}
}
return 0;
}
double copy_arr(double source[][],double target[][],int n,int u)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<u;j++)
{
target[i][j] = source[i][j];
}
}
return target[n][u];
}
Your function prototype is wrong because compiler has not seen n and u yet. Your program does not even compile.
Change
double copy_arr(double source[n][u],double target[n][u],int n,int u);
to
double copy_arr(int n,int u,double source[n][u],double target[n][u]);
Or you could do hardcoded array size
#define SIZE_ARR 5
void copy_arr(double source[][SIZE_ARR], double target[][SIZE_ARR], int n, int u);
int main(void)
{
double source[3][SIZE_ARR]={{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5}};
double target1[3][SIZE_ARR];
copy_arr(source,target1, 3, 5);
int j;
int i;
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
printf("%f 1",target1[i][j]);
}
}
return 0;
}
void copy_arr(double source[][SIZE_ARR], double target[][SIZE_ARR], int n, int u)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<u;j++)
{
target[i][j] = source[i][j];
}
}
}
Related
I was getting blank screen in Devc++ and some random characters in turboc++.
I have to use turbo c++ in lab exam so please help in that way.
Here is code:
#include <stdio.h>
#include<conio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
int part(int arr[],int l,int u)
{
int s=l,e=u;
int pivot=arr[l];
while(s<e)
{
while(arr[s]<=pivot)
{
s++;
}
while(arr[e]>pivot)
{
e--;
}
if(s<e)
{
swap(&arr[s],&arr[e]);
}
}
swap(&arr[l],&arr[e]);
return e;
}
int quick(int arr[],int l,int u)
{
int loc;
if(l<u)
{
loc=part(arr,l,u);
quick(arr,l,loc);
quick(arr,loc+1,u);
}
}
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[] = {0,1,27,8},n;
clrscr();
n = sizeof(arr) / sizeof(arr[0]);
quick(arr,0,n);
printArray(arr,n);
return 0;
}
whats the problem. It executes but doesnt show the correct answer.
It gets compiled as well.
I've used calling function in many places.
its assumed that the matrix is square and i give the input througgh terminal.
for ex=3
then a random 3x3 matrix but the value seems to be incorrect
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void row(int r1,int r2,int n,float A[n][n]){
int c;
float temp[n][n];
for(c=0;c<n;c++){
temp[r1][c]=A[r1][c];
A[r1][c]=A[r2][c];
A[r2][c]=temp[r1][c];
}
}
void maximum(int i, int n,float A[n][n]){
int j;
float max=fabs(A[i][i]);
for(j=i+1;j<n;j++){
if(fabs(A[j][i])>max){
max=fabs(A[j][i]);
row(i,j,n,A);
}
}
}
void op(int k, int n,float A[n][n]){
int i,j;
float f;f
for(i=1;i<n-1-k;i++){
f=-(A[k+i][k]/A[k][k]);
for(j=0;j<n-1-k;j++){
A[k+i][k+j]=A[k+i][k+j]+f*(A[k][k+j]);
}
}
}
int main(){
int i,j,n;
printf("Enter the order of the matrix:");
scanf("%d",&n);
float A[n][n];
for(i=0;i<n;i++){
for(j=0;j<n;j++)
scanf("%f",&A[i][j]);
}
float det=1;
for(i=0;i<n-1;i++){
maximum(i,n,A);
op(i,n,A);
det*=A[i][i];
}
det*=A[n-1][n-1];
printf("%f\n",det);
return 0;
}
I don't know why the error is coming as mentioned in the title. The data are all numbers and the function reads it without problem when tested without pointers.
Note: I don't need to use 'malloc' or any other ones. I'm trying to figure what's going with the structure.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<limits.h>
#define MAX_POINTS 100
#define MAX_POLYGONS 100
typedef struct{
int polyid;
int npoints;
double x[MAX_POINTS];
//Stage1
double y[MAX_POINTS];
double perimeter;
double eccentricity;
double area;
} Polygon;
typedef struct {
Polygon npolygon[MAX_POLYGONS];
}polygons;
//function prototypes
void process_file(polygons *Total_poly);
int largestvalue_index(double A[],int m);
int largest_poly_id(double A[],int m, int B[]);
double area(double x[MAX_POINTS],double y[MAX_POINTS],int n);
double perimeter(double x[MAX_POINTS],double y[MAX_POINTS],int n);
double eccentricity(double area,double perimeter);
int main(int argc, char *argv[]) {
int i,j,k,l;
polygons Total_poly;
process_file(Total_poly);
printf("Stage 1\n");
printf("=====\n");
printf("First Polygon %d\n",Total_poly->npolygon[0]->polyid);
printf("x_val y_val\n");
for(i=0;i<Total_poly->npolygon[0].npoints;i++){
printf("%8.2f %8.2f\n",Total_poly->npolygon[0]->x[i],
Total_poly->npolygon[0]->y[i]);
}
printf("area=%.2f\n",area(Total_poly->npolygon[0].x,
Total_poly->npolygon[0]->y,Total_poly->npolygon[0]->npoints));
printf("perimeter=%.2f\n",perimeter(Total_poly->npolygon[0]->x,
Total_poly->npolygon[0]->y,Total_poly->npolygon[0]->npoints));
printf("eccentricity=%.2f\n",Total_poly->npolygon[0]->eccentricity);
//Stage2
printf("Stage 2");
printf("=======\n");
for(l=1;l<=5;l++){
printf("+-------");
}
printf("+\n");
printf("| id | nval | perim | area | eccen |\n");
for(l=1;l<=5;l++){
printf("+-------");
}
printf("+\n");
for(k=0;k<count;k++){
printf("| %5d | %5d |%6.2f |%6.2f |%6.2f |\n",
Total_poly->npolygon[k]->polyid,Total_poly->npolygon[k]->npoints,
Total_poly->npolygon[k]->perimeter,Total_poly->npolygon[k]->area,
Total_poly->npolygon[k]->eccentricity);
}
for(l=1;l<=5;l++){
printf("+-------");
}
//Stage3
return 0;
}
void process_file(polygons *Total_poly){
int count=0;
int i;
while(scanf("%d %d",&Total_poly.npolygon[count].npoints,
&Total_poly.npolygon[count].polyid)=2){
for(i=0;i<Total_poly.npolygon[count].npoints;i++){
if( scanf("%lf %lf",&Total_poly.npolygon[count].x[i],
&Total_poly.npolygon[count].y[i])=!2)
{
printf("Error");
exit(EXIT_FAILURE);
}else{
scanf("%lf %lf",&Total_poly.npolygon[count].x[i],
&Total_poly.npolygon[count].y[i]);
}
}
count++;
}
}
double area(double x[MAX_POINTS],double y[MAX_POINTS],int n){
int i,j;
double area=0;
j=n-1;
for(i=0;i<n;i++){
area+=(x[i]-x[j])*(y[i]+y[j]);
j=i;
}
return 0.5*fabs(area);
}
double perimeter(double x[MAX_POINTS],double y[MAX_POINTS],int n){
int i,j;
double length=0;
j=n-1;
for(i=0;i<n;i++){
length+=fabs(sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])));
j=i;
}
return length;
}
double eccentricity(double area,double perimeter){
return perimeter*perimeter/area/(4*M_PI);
}
int largest_poly_id(double A[],int m, int B[]){
int i,j;
int poly_index=0,largest = A[0];
for(i=1;i<m;i++){
if(A[i]>largest){
largest=A[i];
poly_index=i;
}
}
j=B[poly_index];
return j;
}
int largestvalue_index(double A[],int m){
int i;
int index=0,largest=A[0];
for(i=1;i<m;i++){
if (A[i]>largest){
largest=A[i];
index=i;
}
}
return index;
}
You are passing a struct to your process_file function. It is, however, expecting a pointer to a struct.
Change this line:
process_file(Total_poly);
to this:
process_file(&Total_poly);
Additionally, you'll need to change the -> operators in the printf statements in the main function to . operators.
I want to pass two matrices as argument. These matrices have different size and i don't understand how i have to do this work:
#include <stdio.h>
#include <stdlib.h>
void f(int m[3][], int n);
int main()
{
int A[3][3]={{1,2,3},{4,5, 6},{7,8,9}};
int B[3][2]={{1,2},{3, 4}, {5, 6}};
f(A, 3);
f(B, 2);
return 0;
}
void f(int m[3][], int n)
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<n;j++)
printf("%5d", m[i][j]);
}
return;
}
How can I do this?
The only safe way that I know of to do this is to include the matrix dimensions in the parameters, or make some kind of matrix struct
Option A) dimensions as parameters
void f(int **m, int w, int h )
{
int i,j;
for(i=0;i<w;i++)
{
for(j=0;j<h;j++)
printf("%5d", m[i][j]);
}
return;
}
Option B) Use a struct
typedef struct Matrix
{
int w, h;
int** m;
} Matrix;
void f ( Matrix *m )
{
for ( int i = 0; i < m->w; ++i )
{
for ( int j = 0; j < m->h; ++j )
{
printf(%5d", m->m[i][j]);
}
}
}
I'm trying to create a simple(?) selection sort program in C that selects the largest integer of an integer array and places it in the location a[n-1], places the second largest number in a[n-2], etc until the smallest number is placed in a[0]. I've run through the below code on paper and it seems like it should work, but when I compile it I'm getting faulty results. Am I missing something obvious?
/* The program implements selection sort*/
#include <stdio.h>
#include "simpio.h"
#define n 5
void GetArray(int a[]);
void SelectionSort(int a[]);
int FindMax(int a[], int high);
void swap(int a[], int p1, int p2);
void PrintArray(int a[]);
main()
{
int a[n];
GetArray(a);
SelectionSort(a);
PrintArray(a);
getchar();
}
void GetArray(int a[])
{
int i;
for(i=0;i<n;i++)
{
printf("Enter integer# %d", i+1);
a[i]=GetInteger();
}
}
void SelectionSort(int a[])
{
int i, max;
for(i=0;i<n;i++)
{
max=FindMax(a,i);
swap(a,max,(n-1-i));
}
}
int FindMax(int a[], int high)
{
int i, index;
index=high;
for(i=high;i<n;i++)
{
if(a[i]>a[index])
index=i;
}
return index;
}
void swap(int a[], int p1, int p2)
{
int temp;
temp=a[p2];
a[p2]=a[p1];
a[p1]=temp;
}
void PrintArray(int a[])
{
int i;
for(i=0;i<n;i++)
printf("a[%d]=%d\n", i, a[i]);
}
Change these method to:
void SelectionSort(int a[])
{
int i, max;
for(i=0;i<n;i++)
{
max=FindMax(a,n-i-1);
swap(a,max,n-i-1);
}
}
int FindMax(int a[], int high)
{
int i, index;
index=high;
for(i=0;i<high;i++)
{
if(a[i]>a[index])
index=i;
}
return index;
}
I actually tested my answer and it works.
Selection sort is process of comparing minimum element from the list and placing from the least index.
Now consider below code snippet.
public void selectionSort(int[] elements) {
for(int i=0;i<elements.length;i++) {
int minPosition = i;
for(int j=i+1;j<elements.length;j++) {
if(elements[minPosition]>elements[j])
minPosition = j;
}
int temp = elements[i];
elements[i] = elements[minPosition];
elements[minPosition] = temp;
}
}
Thanks for reading, let me know feedback to improve from myside
Shouldn't:
max=FindMax(a,i);
swap(a,max,(n-1-i));
Be:
max=FindMax(a,i);
swap(a,max,i);
otherwise, next time through the loop, you'll find the same max value in the top position in the array.
A very basic implementation of selection sort
#include<stdio.h>
main()
{
int i,j,n=7,a[]={1,2,5,3,8,9,5},key;
for(j=1;j<n;j++)
{
key=a[j]; //a[j] is the new element to be added to the sorted
//sequence
i=j-1;
while(i>=0 && key<a[i]) //traverse through the sorted sequence
{a[i+1]=a[i];i--;} //until the place of key is found
a[i+1]=key;
}
for (j=0;j<n;j++)
printf("%d",a[j]);
}
#include<stdio.h>
#include<conio.h>
int removex(int arr[],int small,int n)
{
int i=0;
for(;i<n;i++)
if(arr[i]==small) //searching that no to delete
break;
for(;i<n-1;i++)
arr[i]=arr[i+1]; //delete by overloading no
return n-1;
}
void selectSort(int arr[],int sort[],int n)
{
int j=0,k=0,small;
while(n!=0)
{
small=arr[0];
for(j=0;j<n;j++)
if(arr[j]<small)
small=arr[j]; //finding smallest no
sort[k++]=small;
n=removex(arr,small,n); //removing that from list as we included that no into sorted list
}
}
void main()
{
int arr[10],arr2[10],i,n;
clrscr();
printf("Enter how many elements");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
selectSort(arr,arr2,n);
printf("sorted list is\n");
for(i=0;i<n;i++)
printf("%d\n",arr2[i]);
getch();
}