Write a program which should get 5 floating values from the user in an array using a function insert and then sort these values using a function sort after that print the sorted values on the screen using a function disp.
This is my code please tell me where i have done something wrong.It prints 0.000 after sorting.
#include <stdio.h>
void insert(float array[],int val);
void disp(float array[],int val);
void sort(float array[], int val);
void main ()
{
float array[5],j;
printf("Enter numbers: \n");
insert(array,5); //array input function
printf("Enter numbers are: \n");
disp(array,5); //array output function
sort(array,5); //array sort function
printf("\nSorted Array is: \n");
disp(array,5); //array output function
}
//array input function
void insert(float array[],int val)
{
int k,i;
for (k = 0;k<5;k++){
scanf("%f",&array[k]);
}
}
//array sort function
void sort(float array[], int val){
int i,j;
float hold;
for(i=0; i<6; i++)
{
for(j=0; j<6; j++)
{
if(array[j]>array[j+1])
{
hold=array[j];
array[j]=array[j+1];
array[j+1]=hold;
}
}
}
}
//array display function
void disp(float array[],int val)
{
int k;
for (k = 0;k<5;k++){
printf("%f\n",array[k]);
}
}
Sort function should look like this:
//array sort function
void sort(float array[], int val)
{
int i,j;
float hold;
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
if(array[j]>array[j+1])
{
hold=array[j];
array[j]=array[j+1];
array[j+1]=hold;
}
}
}
}
In the original code first loop executes two times too many.
Second loop causes reading beyond the array. Since you have 5 elements you need 4 comparisons.
First of all, you declared some variables that you didn't use, especially int val in your functions.
Secondly, you don't need to implement a function to insert values in an array, just loop it with a scanf directly.
Related
in a program to accept an array and display it on the console using functions getArray() and displayArray() how this program works as it Accepts values of array in function getArray() and uses it in the function displayArray() without returning any values from the first function?
I tried this program and failed to get result, then found this one in youTube comment section and I tried it and got results! I want to know how this program works ?
Q:Write a program to accept an array and display it on the console using function?
a.Program should contain 3 functions including main() function,
main() - {
Declare an array.
Call function getArray().
Call function displayArray() }.
getArray() -
Get values to the array.
displayArray() -
Display the array values
#include <stdio.h>
#include <stdlib.h>
void getArray(int);
void displayArray(int);
int main(void) {
int limit;
printf("Enter The Size of the Array :");
scanf("%d",&limit);
getArray(limit);
displayArray(limit);
return EXIT_SUCCESS;
}
void getArray(int limit){
int i,a[100];
printf("Enter The Values of Array :\n");
for(i=0;i<limit;i++){
scanf("%d",&a[i]);
}
}
void displayArray(int limit){
int i,b[100];
printf("Your Array is :\n");
for(i=0;i<limit;i++){
printf(" %d\t",b[i]);
}
printf("\n");
}
Array a in getArray is a local variable that gets destroyed when it goes out of scope. Array b in displayArray is also a local variable (local to displayArray) and has no relationship with a in getArray. You need to pass the same array to both functions.
One way could be to allocate the needed memory for the array in main and pass that, along with the number of elements in the array, to the two functions.
Example:
#include <stdio.h>
#include <stdlib.h>
// a is now a pointer to the first element in the array:
void getArray(int *a, int limit) {
printf("Enter The Values of Array :\n");
for(int i = 0; i < limit; i++) {
scanf("%d", &a[i]);
}
}
// b is now a pointer to the first element in the array:
void displayArray(int *b, int limit) {
printf("Your Array is :\n");
for(int i = 0; i < limit; i++) {
printf(" %d\t", b[i]);
}
putchar('\n');
}
int main(void) {
int limit;
printf("Enter The Size of the Array :");
if(scanf("%d", &limit) != 1 || limit < 1) return EXIT_FAILURE;
// allocate memory for `limit` number of `int`s:
int *arr = malloc(limit * sizeof *arr);
if(arr == NULL) return EXIT_FAILURE;
getArray(arr, limit); // pass arr + limit
displayArray(arr, limit); // pass arr + limit
free(arr); // and free the memory when done
return EXIT_SUCCESS;
}
#include<stdio.h>
void getArray(int);
void displayArray(int);
int array[100];
void main()
{
int limit;
printf("enter the array size you want\n");
scanf("%d",&limit);
getArray(limit);
displayArray(limit);
}
void getArray(int limit)
{
printf("enter the array element you want\n");
for(int i=0;i<limit;i++)
{
scanf("%d",&array[i]);
}
}
void displayArray(int limit)
{
for(int i=0;i<n;i++)
{
printf("%d",array[i]);
printf("\t");
}
}
I have two functions. One that creates a multiplication table of a given number and the other function prints the array out. Below is my code:
Here's the error (Line 18):
expression must be a pointer to a complete object type
How do I fix this error and print the array? Also, I don't know how to print a new line after every row.
#include "multiplication.h"
#include <stdio.h>
int arr[][];
void mulitpication(int num){
/* initialize array and build*/
int arr[num][num];
for(int i=0; i<num;i++){
for(int j=0;j<num;j++){
arr[i][j]= (i+1)*(j+1);
}
}
}
void print(int arr[][]){
/* print the arr*/
int i;
for(i=0;i<sizeof(arr);i++){
for(int j=0;j<sizeof(arr);j++){
printf("%d ",arr[i][j])**(line 18)**;
}
}
}
If using C99 or later with VLA support, pass into the print function the dimensions needed.
// void print(int arr[][]){
void print(size_t rows, size_t cols, int arr[row][col]){
size_t r,c;
for (size_t r = 0; r < rows; r++) {
for (size_t c = 0; c < cols; c++) {
printf("%d ",arr[r][c]);
}
printf("\n");
}
}
You need to declare the array in main(), so it can be passed to both functions.
When an array is passed as a function parameter, it just passes a pointer. You need to pass the array dimensions, they can't be determined using sizeof.
To get each row of the table on a new line, put printf("\n"); after the loop that prints a row.
#include <stdio.h>
void multiplication(int num, arr[num][num]){
/* initialize array and build*/
for(int i=0; i<num;i++){
for(int j=0;j<num;j++){
arr[i][j]= (i+1)*(j+1);
}
}
}
void print(int num, int arr[num][num]){
/* print the arr*/
int i;
for(i=0;i<num;i++){
for(int j=0;j<num;j++){
printf("%d ",arr[i][j]);
}
printf("\n");
}
}
int main(void) {
int size;
printf("How big is the multiplication table? ");
scanf("%d", &size);
int arr[size][size];
multiplication(size, arr);
print(size, arr);
return 0;
}
according to the book "introduction to algorithms I tried sorting a matrix using column sort.here is my approach
1) sort each row of matrix -
#include<stdio.h>
#include<stdlib.h>
void sort(int arr[3][3], int k)// here k defines the column number ,k remains constant through out the function
//because we have to sort the matrix column wise respectively
{
int i;
int c[10]={0};
int b[3][3];
for(i=0;i<3;i++)
{ c[arr[i][k]]++;
}
for(i=1;i<3;i++)
{
c[i]+=c[i-1];
}
for(i=2;i>=0;i--)
{
b[c[arr[i][k]]-1][k]=arr[i][k];
c[arr[i][k]]--;
}
for(i=0;i<3;i++)
{
arr[i][k]=b[i][k];
}
}
I have passed k as an argument,which is the column number (as it remains constant throughout the sorting of each column, and only the row number changes,so i have iterated only over the row number )
the function to pass the desired column number which then calls the count function
void column(int arr[3][3]) // to call the function column wise by passing k as a parameter to count function
{
int k;
for(k=0;k<3;k++)
sort(arr,k);
}
2) transpose a matrix
{
int i,j,temp;
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
temp=arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp;
}
}
the print function
void print(int arr[3][3]) // to print the output
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
}
the main function
{
int arr[3][3];
int i,j;
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d ",&arr[i][j]);
}
// prints the array just inputed
print(arr);
// column calls the function sort according to the column
column(arr);
transpose(arr); // matrix is transposed
printf("\n");
print(arr); // matrix is printed
column(arr); // matrix is again passed with respect to the columns and sorted
transpose(arr); // matrix is transposed to get the original matrix
printf("\n");
print(arr); //final result is printed
return 0;
}
the output is very unlikely how can it be sorted correctly
Here is a working version of your code. I fixed the dropped bracket and the transpose function:
#include <stdio.h>
#include<stdlib.h>
void sort(int arr[3][3], int k)// here k defines the column number ,k remains constant through out the function
//because we have to sort the matrix column wise respectively
{
int i,j;
int tmp = 0;
for(i=0;i<2;i++)
{
for (j = 0; j < 3-i-1; j++)
{
if (arr[j][k] > arr[j+1][k])
{
tmp = arr[j][k];
arr[j][k] = arr[j+1][k];
arr[j+1][k] = tmp;
}
}
}
printf("\n");
}
void column(int arr[3][3]) // to call the function column wise by passing k as a parameter to count function
{
int k;
for(k=0;k<3;k++)
{
sort(arr,k);
}
}
void transpose(int arr[3][3])
{
int i,j,temp;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i != j && i<j)
{
temp=arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp;
}
}
}
}
void print(int arr[3][3]) // to print the output
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
}
int main()
{
int arr[3][3] = {0};
int i,j;
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d ",&arr[i][j]);
}
printf("\n");
// prints the array just inputed
print(arr);
printf("\n");
// column calls the function sort according to the column
column(arr);
print(arr);
transpose(arr); // matrix is transposed
printf("\n");
print(arr); // matrix is printed
column(arr); // matrix is again passed with respect to the columns and sorted
printf("\n");
print(arr); // matrix is printed
transpose(arr); // matrix is transposed to get the original matrix
printf("\n");
print(arr); //final result is printed
return 0;
}
Here is the output of: Sort, Transpose, Sort, then transpose:
im trying to pass a 2D array from main to a function and trying to print it letter by letter
but it keeps giving me segmentation fault
note: the question im trying to solve as mentioned a function with parameter { ArrPrintMatrix(char *(p)[7]) }
so help me by keeping the above thing in mind
#include<stdio.h>
ArrPrintMatrix(char **p,int n) {
int i,j;
for(i=0;i<n;i++) {
for(j=0;j<10;j++) {
printf("%c ",p[i][j]);
}
}
}
main() {
int i;
char c[2][10];
puts("enter two strings");
for(i=0;i<2;i++)
scanf("%s",c[i]);
ArrPrintMatrix((char **) c,2);
}
You should use char p[2][10] not char** p
The following code could work:
#include <stdio.h>
void ArrPrintMatrix(char p[2][10], int n) {
int i;
for (i = 0; i < n; ++i)
printf("%s\n",p[i]);
}
int main() {
int i;
char c[2][10];
puts("enter two strings");
for(i=0;i<2;i++)
scanf("%s",c[i]);
ArrPrintMatrix(c,2);
return 0;
}
you need to change the type of the p var in the print function, and you should also set the array to zero so if the strings that are printing are less than 10 chars with terminator- garbage values are not displayed.
void ArrPrintMatrix(char p[][10],int n) {
int i,j;
for(i=0;i<n;i++) {
for(j=0;j<10;j++) {
printf("%c ",p[i][j]);
}
}
}
int main() {
int i;
char c[2][10]= {0};
puts("enter two strings");
for(i=0;i<2;i++)
scanf("%s",c[i]);
ArrPrintMatrix( c,2);
return 0;
}
The program I"m trying to finish is a program using the bubble sort algorithm. I am not sure what is the problem or in which function the problem is in. The problem is the program does not sort the array in properly. (It also must be arranged in ascending order).
Here is the code:
#include <stdio.h>
#include "simpio.h"
void getArray (int arr[], int size);
void sortArray (int arr[], int size);
void swap (int arr[], int num, int number);
void dispArray (int arr[], int size);
bool checkBigger (int arr[], int num, int number);
main()
{
int size;
printf("Enter number of elements: ");
size=GetInteger();
int arr[size];
getArray(arr, size);
sortArray(arr, size);
dispArray(arr, size);
getchar();
}
void getArray (int arr[], int size)
{
int num;
printf("Please enter the value of the elements: \n");
for(num=0; num<size; num++)
{
arr[num]=GetInteger();
}
}
void sortArray (int arr[], int size)
{
int num, number, d;
for(num=0;num<size-1;num++)
{
for(d=0; d<size-num-1; d++)
{
number=num+1;
checkBigger(arr, num, number);
}
}
}
void swap (int arr[], int num, int number)
{
int tem;
tem=arr[num];
arr[num]=arr[number];
arr[number]=tem;
}
void dispArray (int arr[], int size)
{
int num;
printf("The sorted list is:\n");
for(num=0; num<size; num++)
{
printf("%d\t", arr[num]);
}
}
bool checkBigger (int arr[], int num, int number)
{
if(arr[num]>arr[number])
{
swap(arr, num, number);
}
}
Thank you very much.
void sortArray (int arr[], int size)
{
int num, number, d;
for(num=0;num<size-1;num++)
{
for(d=0; d<size-num-1; d++)
{
number=d+1;
checkBigger(arr, d, number);
}
}
}
pretty sure your problem is with you algorithm, try to simulate your algorithm in pen and paper. it will help your understanding of your code and the algorithm better :)
for your convenience here i am including a bubble sort algorithm i did some while ago
void bubbleSort( int a[], int n)
{
int i,j,temp; // for a={1,2,3,4,5} n is 5
n = n - 1; // bcz otherwise it will get out of index
for(i=0; i<n; i++)
{
for(j=0; j<n-i; j++)
{
if(a[j]>a[j+1])
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
}
i hope this helps
All I follow from the above examples is an implementation of the exchange sort.
The exchange sort on the outer loop checks each entry in the table against the first element, exchanging when necessary. At then end of the inner loop, the lowest element is in position 1, then it begins with position 2, comparing it to the remaining elements, and doing an exchange. Even if the array was already in order, the sort cannot stop. It has to do a n*(n-1) compares. An array of 50 elements, already sorted will do 50*49 comparisons.
The bubble sort works differently
set a swap flag to zero. Then
slide along the array, comparing position(i) to position(i+1). If a swap takes place, you do the sort again.
here is some pseudo code.
swap = 0
do {
for (i=o;i< no-elements-1;i++) {
if (array[i] > array[i+1])
{
do the exchange
set swap=1
}
/**/
} while (swap == 1);
The above illustrates the bubble sort.
Note. if the data is in order, there is no swap and there is no second loop. The sort algorithm is able to quit early.
if a fifty element array is in order, the sort would have done 50 comparisons and would have stopped.
The exchange sort, which is described earlier would have to do 50*49 or 2450 comparisons.
// BUBBLE SORT.
#include <stdio.h>
#define MAX 20
int main()
{
int arr[MAX];int no;
printf("PLEASE ENTER THE CURRENT SIZE OF THE ARRAY\n");
scanf("%d",&no);
int i;
printf("PLEASE ENTER THE ELEMENTS OF THE ARRAY\n");
for(i=0;i<no;i++)
scanf("%d",&arr[i]); /*reading the elements*/
/* sorting begins*/
int j,k,l;
int temp;
int flag=0;
for(k=0;k<no-1;k++)
{
flag=0;
j=k;
for(i=0;i<no-j-1;i++) /* not going to the part that has been sorted*/
{
if(arr[i]>arr[i+1])
{
flag=1;
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
else
continue;/* not necessary*/
}
if(flag==0) /*implies that the array is alraedy sorted*/
break;
}
printf("THE SORTED LIST:\n\n");
for(i=0;i<no;i++)
printf("%d\n",arr[i]);
}