Here is my. I think my code is correct but it gets stuck after i give input. But if i remove other code except sorting and printing it in ascending order it works. But if not it doesn't work.
It stuck here
#include <stdio.h>
#include <math.h>
float dplus(float num[], int n);
float dminus(float num[], int n);
float larges(float data[], int n);
int main()
{
printf("Kolmogorov Test\n");
int n;
float dvalue1;
//printf("No. of elements should not be greater than 20.\n");
printf("Enter number of elements to compute for tets: \t");
scanf("%d", &n);
float num[n];
float dp, dn;
for(int i=0; i<n; i++)
{
scanf("%f", &num[i]);
}
//sorting in ascending order
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
if(num[i]>num[j])
{
float temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
printf("\nNumbers in ascending order is: \t");
for(int i=0; i<n; i++)
{
printf("%0.2f\t",num[i]);
}
dp = dplus(&num[n], n);
dn = dminus(&num[n], n);
if(dp>dn)
{
dvalue1 = dp;
}
else
{
dvalue1 = dn;
}
//float dalphas = 0.05;
float dvalue = 0.565;
if(dvalue1 < dvalue)
{
printf("\n Since D is less tha Dalpha so the data is unformily distributed.");
}
else
{
printf("\nSince D is greater than Dalpha so the data is not uniformily distributed.");
}
return 0;
}
float dplus(float num[], int n)
{
float data[n];
int count=1;
for(int i=0; i<n; i++)
{
while(count<=n)
{
data[i] = ((count/n)-num[i]);
}
}
float lar = larges(&data[n], n);
return lar;
}
float dminus(float num[], int n)
{
float data[n];
int count=1;
for(int i=0; i<n; i++)
{
while(count<=n)
{
data[i] = ((count/n)-num[i]);
}
}
float lar;
lar = larges(&data[n], n);
return lar;
}
float larges(float data[], int n)
{
for(int i=1; i<n; i++)
{
if(data[0]<data[i])
data[0] = data[i];
}
float lar = data[0];
// printf("%f",lar);
return lar;
}
in the while loop which you used to initiating functions d plus and d minus you used while loop which is going for a infinite loop as the count value is not changing in the loop.It is going on and on and on.
You should always check the return value from scanf. That is - scanf returns the number of items matched, so you should do:
if (1 != scanf("%d", &n))
{
// Add error handling
}
If you want to repeat until the input is matched, do:
while(1)
{
printf("Enter number of elements to compute for tets: \t");
if (1 == scanf("%d", &n)) break;
printf("\nIllegal input, try again\n");
}
The same applies when reading in the float values.
That said, I don't think this is the reason, your program is stuck. To debug it, just add more printf statements. Example:
printf("Enter number of elements to compute for tets: \t");
scanf("%d", &n);
printf("Got %d\n", n); // DEBUG PRINT
float num[n];
float dp, dn;
for(int i=0; i<n; i++)
{
scanf("%f", &num[i]);
}
printf("Got all floats\n"); // DEBUG PRINT
//sorting in ascending order
for(int i=0; i<n; i++)
{
printf("Got i = %d\n", i); // DEBUG PRINT
for(int j=i+1; j<n; j++)
{
if(num[i]>num[j])
{
float temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
and so on.
Related
I have to find out if there is any pair of i,j such that array[i]^2 + array[j]^2 == x^2
.
If there are such pairs, I need to print all such (i,j). Otherwise, print “There are no such pairs”.
#include <stdio.h>
int main(){
int size=10, i, x,j;
int Array[size];
printf("What is the value of x:");
scanf("%d",&x);
for(i=0;i<size;i++){
printf("Enter array value :");
scanf("%d",&Array[i]);
}
for(i=0;i<size;){
for(j=i+1;j<size;j++)
if((Array[i]*Array[i])+(Array[j]*Array[j])==x*x) //how do I complete this for loop?
}
return 0;
}
Yo're almost there, why weren't you incrementing the value of i? Keep a counter to count the matched pairs, then print those or if nothing is found print whatever you want.
#include <stdio.h>
int main() {
int size = 10, i, x, j;
int Array[size];
printf("What is the value of x:");
scanf("%d", &x);
for (i = 0; i < size; i++) {
printf("Enter array value :");
scanf("%d", &Array[i]);
}
int counter = 0;
for (i = 0; i < size; i++) {
for (j = i + 1; j < size; j++)
if ((Array[i] * Array[i]) + (Array[j] * Array[j]) == x * x) {
printf("%d %d\n", Array[i], Array[j]);
counter++;
}
}
if (!counter) {
printf("There are no such pairs\n");
}
return 0;
}
This program is designed to essentially take user input of grades and output various functions listed. The main problem I have with this is that my pointer (processGrades) to functions will not work. It outputs the error "Called object type 'void' is not a function or function pointer". Is this not the correct implementation of pointers? What might be causing this?
#include <stdio.h>
void minimum(int students, int exams, int studentGrades[students][exams])
{
int lowGrade = 100;
size_t i, j;
for (i = 0; i < students; ++i) {
for (j = 0; j < exams; ++j) {
if (studentGrades[i][j] < lowGrade) {
lowGrade = studentGrades[i][j];
}
}
}
printf("Minimum grade : %d\n", lowGrade); // return minimum grade
return;
}
void maximum(int students, int exams, int studentGrades[students][exams])
{
int highGrade = 0; // initialize to lowest possible grade
size_t i, j;
for (i = 0; i < students; ++i) {
for (j = 0; j < exams; ++j) {
if (studentGrades[i][j] > highGrade) {
highGrade = studentGrades[i][j];
}
}
}
printf("Maximum grade : %d\n", highGrade); // return maximum grade
return;
}
void average(int students, int exams, int studentGrades[students][exams])
{
double total = 0.0; // sum of test grades
size_t i, j;
for (i = 0; i < students; ++i) {
for (j = 0; j < exams; ++j) {
total += studentGrades[i][j];
}
printf ("Average of student %lu : %f\n", i, total / exams); // average
total = 0;
}
return;
}
void printArray(int students, int exams, int studentGrades[students][exams])
{
size_t i, j;
printf("%s", " [0] [1] [2] [3]");
for (i = 0; i < students; ++i) {
printf("\nstudentGrades[%lu] ", i);
for (j = 0; j < exams; ++j) {
printf("%-5d", studentGrades[i][j]);
}
}
printf("\n");
return;
}
int main(void)
{
int choice;
int students;
int exams;
printf("Let's create a 2Dim array!\n\n");
printf("How many students? ");
scanf("%d", &students);
printf("\nHow many exams? ");
scanf("%d", &exams);
printf("\n");
int studentGrades[students][exams];
int i;
int j;
int entry;
for (i = 0; i < students; i++){
for (j=0; j < exams; j++) {
printf("enter [%d] [%d]: ", i,j);
scanf("%d",&entry);
studentGrades[i][j] = entry;
}
}
void (*processGrades[4]);
processGrades[0] = printArray;
processGrades[1] = minimum;
processGrades[2] = maximum;
processGrades[3] = average;
int stop = 0;
while(stop != 1) {
printf("Enter a choice:\n");
printf("0 Print the array of grades\n");
printf("1 Find the minimum grade\n");
printf("2 Find the maximum grade\n");
printf("3 Print the average on all tests for each student\n");
printf("4 End Program\n");
scanf("%d", &choice);
if(choice == 4){
stop = 1;
}
else{
(*processGrades[choice])(students)(exams)(studentGrades));
}
}
}
You can typedef a function pointer of the type of your process functions:
typedef void (*processGradesFnPtr)(int, int, int [*][*]);
And in the main, you can an declare an array of four function pointers and assign them accordingly.
processGradesFnPtr processGrades[4];
processGrades[0] = printArray;
processGrades[1] = minimum;
processGrades[2] = maximum;
processGrades[3] = average;
And when you call them, you have to call them like this:
(*processGrades[choice])(students, exams, (studentGrades));
#include<stdio.h>
int main()
{
int n;
printf("Enter the size of array");
scanf("%d",&n);
int array[n-1];
printf("Enter the elements of array");
for(int i=0; i<n; i++)
{
scanf("%d",&array[i]);
}
int product = 0;
for(int i=0; i<n; i++)
{
Here I put the condition that, when all unique products are over then exit the loop and print the output.
if(i>(n-1-i))
{
break;
}
int f = array[i];
int l = array[n-i];
product = product + (f*l);
}
printf("Result: %d",product);
return 0;
}
For an array of {1,2,3,4,5,6,7,8,9,10}, I got the result as 4206199. Right Ans is 110.
Your program should be like this
#include <stdio.h>
int main(void)
{
int n;
printf("Enter the size of array: ");
scanf("%d",&n);
int array[n]; // Here it should be "n" not "n-1"
printf("Enter the elements of array: ");
for(int i=0; i<n; i++)
{
scanf("%d",&array[i]);
}
int product = 0;
for(int i=0; i<n; i++)
{
if(i>(n-1-i))
{
break;
}
int f = array[i];
int l = array[n-i-1]; // Here it should be n-i-1
product = product + (f*l);
}
printf("Result: %d\n",product);
return 0;
}
I wrote this program to calculate the scalar product and find out the smallest as well as the biggest number (+ there position) of the vector v. Everything works fine except for the lowest_number function. It find the lowest number inside the vector v and the position as well,but when the position is at v[0] ----> at 1 it displays a really long number. I am not sure why this happens. PLease help thank you very much
int scalar_produc(int *v,int *w,int n)
{
int i = 0;
int sp;
int sp2 = 0;
for(i= 0; i < n; i++)
{
sp = v[i] * w[i];
sp2 = sp2 + sp;
}
return sp2;
}
int lowest_number(int *v,int n)
{
int i = 0;
int low = v[i];
int position;
for(i = 0; i < n; i++)
{
if(v[i] < low)
{
low = v[i];
position = i;
}
}
printf("The lowest number is: %d \n", low);
printf("The position of it: %d \n",position+1);
}
int biggest_number(int *v, int n)
{
int i = 0;
int position;
int biggest = v[i];
for(i = 0; i < n; i++)
{
if(v[i] > biggest)
{
biggest = v[i];
position = i;
}
}
printf("The biggest number of the vector 'v' is: %d \n",biggest);
printf("The position of the biggest number: %d \n", position+1);
}
int main()
{
int n,i;
int *v,*w;
printf("Enter the number of vectors you would like to enter: \n");
scanf("%d",&n);
v = (int*) malloc(sizeof(int) *n);
w = (int*) malloc(sizeof(int) *n);
printf("Enter the vectors: \n");
for(i = 0; i < n ;i++)
{
printf("v[%d]: \n",i+1);
scanf("%d",&v[i]);
printf("w[%d]: \n",i+1);
scanf("%d",&w[i]);
}
printf("The scalar product: %d \n",scalar_produc(v,w,n));
lowest_number(v,n);
biggest_number(v,n);
free(v);
free(w);
return 0;
}
You don't initialize position. So if the smallest value is the first value, the value of position is undefined.
Initialize it to 0 and it should work.
int i = 0;
int low = v[i];
int position = 0;
This program is supposed to take 2 arrays and perform the dot product on each of the elements in the array.
My program is fine if the index of n is less than 5; however, once the index of the array is greater than 5 only the first element in the first array is wrong ( I checked by adding a printf statement in the function). I don't know how to fix this bug.
#include <stdio.h>
void multi_vec(int *v1, int *v2, int *v3, int n);
int main(void)
{
int n, i;
int v1[n];
int v2[n];
int v3[n];
printf("Enter the length of the two vectors\n");
scanf("%d", &n);
printf("Enter numbers for the first array\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &v1[i]);
}
printf("Enter numbers for the second array\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &v2[i]);
}
multi_vec(v1, v2, v3, n);
for (i = 0; i < n; i++) {
printf("%d", v3[i]);
}
printf("\n");
return 0;
}
void multi_vec(int *v1, int *v2, int *v3, int n)
{
int i;
for (i = 0; i < n; i++) {
*(v3+i) = *(v1+i) * *(v2+i);
}
}
Correct code
#include <stdio.h>
void multi_vec(int *v1, int *v2, int *v3, int n);
int main(void)
{
int n, i;
printf("Enter the length of the two vectors\n");
scanf("%d", &n);
int v1[n],v2[n],v3[n]; //you didn't initialize n
printf("Enter numbers for the first array\n"); //printf statements had extra ',n'
for (i = 0; i < n; i++) {
scanf("%d", &v1[i]);
}
printf("Enter numbers for the second array\n"); //printf statements had extra ',n'
for (i = 0; i < n; i++) {
scanf("%d", &v2[i]);
}
multi_vec(v1, v2, v3, n);
for (i = 0; i < n; i++) {
printf("%d ", v3[i]);
}
printf("\n");
return 0;
}
void multi_vec(int *v1, int *v2, int *v3, int n)
{
int i;
for (i = 0; i < n; i++) {
*(v3+i) = *(v1+i) * *(v2+i);
}
}