The code I have so far for setting up my array is this:
#include <stdio.h>
void printArray(float myArray[4][3]);
int main(void)
{
};
printArray(sides);
return 0;
}
void printArray(float passedArray[4][3])
{
printf("Side A\tSideB\tSide C\n");
for (int x = 0; x < 4; x++)
{
for (int y = 0; y < 3; y++)
{
printf("%.3f \t", passedArray[x][y]);
}
printf("\n");
}
}
I've also created a way to evaluate the hypotenuse in a previous code if I was given an input from the user:
#include <stdio.h>
#include <math.h>
double hypotenuse(double lengtha, double lengthb);
int main() // Start of main function
{
double lengtha, lengthb; //storing variables for later use
printf("Enter the length of side A: \n"); //Prompt user for input of A
scanf("%lf", &lengtha); //Stores input from user
printf("Enter the length of side B: \n\n"); // Prompt user for input of B
scanf("%lf", &lengthb); //Stores input from user
return 0; // terminate
} /* End function main */
double hypotenuse(double sidea, double sideb)
{
return sqrt(pow(sidea, 2) + pow(sideb, 2));
} /* End function */
The main issue that' I'm running into however though, is I'm unsure how to take the pre stored values from my first code/arrays, throw them into the equation, then have them output into side c into the table. I know there has a to be a way, but it's really hard to find too much info since C is a bit older. Any suggestions or help would be greatly appreciated!
If i understood you correctly, you could just iterate over every row of your array and assign the result of the function call to the last column:
for (int i = 0; i < 4; ++i) {
array[i][2] = hypotenuse(array[i][0], array[i][1]);
}
Related
I just started learning C language. So, I am running into a lot of problems. I thought declaring i under for loop is enough, and I can use the value of i for outside too. But I think, that was not the case. Can someone explain the situation, please.
# include <stdio.h>
int main(void)
{
int x;
printf("Enter how many numbers in arrays you want to input : ");
scanf("%i", &x);
int score[x];
for(int i= 0; i <= x; i++)
{
printf("Enter the score : ");
scanf("%i", &score[i]);
}
// in the below line the output said "i" is undeclared.
float average = score[i] / x;
printf("The average score is : %f", average);
}
The answer is fairly simple
because of where you decalred i it is only visable to the for loop.
To make i visable to the whole function all you need to do is:
int i = 0;
for (; i <=x; i++){
printf("Enter the score : ");
scanf("%i", &score[i]);
}
this makes i avaliable throughout the function
i is declared in the initialization section of a for statement. That means the scope and lifetime of that variable is the expressions in the for statement itself as well as the block statement it contains. Once the loop is done, the variable no longer exists.
You need to declare i outside of the loop if you want to use it outside of the loop.
int i;
for(i= 0; i <= x; i++)
That being said, you don't actually need to use i outside of the loop.
There are security issues associated with using scanf so don't use it for anything serious. That said, I tried to re-write your program properly, and it still has pretty rubbish input validation.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INPUTTEXTLEN 20
#define MAXINPUTINT 1000
int inputint() {
char inputtext[INPUTTEXTLEN + 1] = {0};
long inputval;
while (1) {
fgets(inputtext, INPUTTEXTLEN, stdin);
if (strlen(inputtext) > 0) {
inputval = atoi(inputtext);
if ((inputval < MAXINPUTINT) && (inputval >= 0)) break;
}
}
return (int)inputval;
}
int main(void)
{
int x = 0;
printf("Enter how many numbers in arrays you want to input : ");
//scanf("%i", &x);
while (x <= 0) {
x = inputint();
}
int score[x];
float average = 0;
for(int i= 0; i < x; i++)
{
printf("Enter the score : ");
//scanf("%i", &score[i]);
score[i] = inputint();
average += score[i];
}
average /= x;
printf("The average score is : %f\n", average);
}
I am new to C and was making a program where a hash grid is drawn and the user inputs the dimensions of a grid. I also used the cs50 Library to get a int. When ever I input the dimensions, no hashes show up. Please help me. Thanks in advance
Code:
#include <stdio.h>
#include <cs50.h>
int main(void){
int x;
int y;
do{
x=get_int("Width of the hash floor: ");
y=get_int("Length of the hash floor: ");
return x;
return y;
} while (x>1);
for (int n=0;n<x;n++){
printf("#");
for(int a=0;a<y;a++){
printf("#\n");
}
}
}
You probably want this:
remove both return statements, they don't make any sense here.
change the while loop (see comment in the code below).
#include <stdio.h>
#include <cs50.h>
int main(void) {
int x;
int y;
do{
x = get_int("Width of the hash floor: ");
y = get_int("Length of the hash floor: ");
} while (x < 1 || y < 1); // ask for width and length until both
// x and y are larger than 0
for (int n = 0; n < x; n++) {
printf("#");
for (int a = 0; a < y; a++) {
printf("#\n");
}
}
}
in the do while ...
Don't use return and let the condition of while fulfill.
The Return instruction is used to return to the Main Program from a Subroutine Program or Interrupt Program
So, I have this code which I need to turn into a function:
int main(void) {
int i=0,seed;
printf("\n\nEnter seed integer value: ");
scanf("%d", &seed);
printf("\nSeed value is:%d\n\n",seed);
srand(seed);
int a[5][5];
int x,y;
printf("Matrix A:\n");
for(x=0;x<5;x++) {
for(y=0;y<5;y++) {
a[x][y] = rand() %51 + (-25);
printf("%d ",a[x][y]); }
printf("\n"); }
printf("\n\n");
So basically, it produces a 2D 5x5 array of random numbers. This works fine, however my next task is applying a function to this code, with the function name of:
void generate_matrices(int a[5][5])
I have tried multiple times, the closest I got to a successful code was:
#include <stdio.h>
#include <stdlib.h>
void generate_matrices(int a[5][5]);
int main(void) {
int a, seed;
printf("\n\nEnter seed integer value: ");
scanf("%d", &seed);
srand(seed);
printf("\nSeed value is:%d\n\n",seed);
generate_matrices(a);
return 0;
}
void generate_matrices(int a[5][5]) {
int y,z;
printf("Matrix A:\n");
for(y=0;y<5;y++) {
for(z=0;z<5;z++) {
a[y][z] = rand() %51 + (-25); }
printf("%d ",a[y][z]); }
printf("\n");
}
But this returns the error, "expected 'int(*)[5]' but arguement is of type 'int'.
All/any help is muchly appreciated. To be fair on my part, I have done 90% of the code. This is the only bit I need help with so that I can apply this to the rest of my code.
Cheers!
You have declared a as a single integer on this line int a, seed;
When you call the function with generate_matrices(a); you are passing a single integer instead of a pointer to an array.
Change your declaration line to int a[5][5], seed;
generate_matrices(a); will pass a pointer to the first element in your 5 * 5 array, to the function.
You should really print the results in main and not in the function, then you will know that the array has been modified and is available for use in the body of your program.
You have used unconventional placement of braces '}' and this makes it harder to see what belongs in each part of your for loops.
You have the print statements in the wrong places - as a result only part of the matrix is printed.
This is what it should be (just the results - in main):
printf("Matrix\n ");
for (y = 0; y < 5; y++) {
for (z = 0; z < 5; z++) {
printf("%d\t ", a[y][z]);
}
printf("\n");
}
If you use int a[5][5] and call the function with generate_matrices(a);
a function void generate_matrices(int a[5][5]) {...} compiles without error
#include<stdio.h>
#include<stdlib.h>
void modify(int b[5][5]);
int main()
{
srand(4562);
int i,j,arr[5][5];
modify(arr);
for(i=0;i<5;i++){
for(j=0;j<5;j++){
printf("%d ",arr[i][j]=rand() %51 + (-26)); }
}
return 0;
}
void modify(int b[5][5])
{
int i,j;
for(i=0;i<5;i++) {
for(j=0;j<5;j++) {
b[i][j]; }
}
}
So this is the closest I have come to completing it. It produces the number of elements I want, also within the range I want. However its not producing the 5x5 grid I need. Where have I gone wrong?
EDIT: I'm not going for neatness at the moment, I just want to get the program working how I want it too and then i'll neaten it up.
EDIT 2: Never mind, realised what I didn't include. Its fine now. Thanks for the help.
I'm trying to write a program in C that finds prime numbers and calls a function. However I keep getting a singular error that says the called object 'is_prime' is not a function in line 22. I set up a prototype but it still doesn't work. Help!
#include<stdio.h>
double is_prime(int x);
int main()
{
double is_prime;
double primenum = 0;
int n=0;
int x=0;
printf("Enter the value of n: "); //getting length//
scanf("%d",&n);
printf("\n Printing primes less than or equal to %d: \n", n); //message//
for(x = 2; x <= n; x++) //loop to check and print prime #s//
{
primenum = is_prime(x); <------------line 22
if (primenum == 1)
{ printf("%d, ",x); }
}
return 0;
}
double is_prime(int x)
{
int i=0;
for (i = 2; i < x; i++) // this is the loop to check all numbers//
// under n to see if prime//
{
if (x==2)
{ return 1;}
if ((x%i) == 0)
{return 1;}
else
{return 0;}
}
}
You have declared a variable with the same name as the function:
double is_prime;
This shadows the name of the function from the point of the declaration to the end of the scope of main.
Use a different name for either, or remove the declaration above, since you don't seem to need it.
You define double is_prime;, so is_prime becomes a double, not a function.
I'm trying to calculate the size of the file . The process I've followed is to read the file and store it in an array and calculate its size. However,I really don't know ... I tried n number of ways..I've to pass this size as an attribute to the frequency function.along with the name of the array.
#include <stdio.h>
#include<conio.h>
void frequency (int theArray [ ], int ??????, int x)
{
int count = 0;
int u;
for (u = 0; u < ??????; u++)
{
if ( theArray[u]==x)
{
count = count + 1 ;
/*printf("\n%d",theArray[u]);*/
}
else
{
count = count ;
}
}
printf ("\nThe frequency of %d in your array is %d ",x,count);
}
void main()
{
FILE*file = fopen("num.txt","r");
int integers[100];
int i=0;
int r = 0;
int num;
int theArray[100];
int there[100];
int n;
int g;
int x;
while(fscanf(file,"%d",&num)>0)
{
integers[i]=num;
printf("\n%d",(integers[i]));
there[r] = integers[i];
i++;
}
//printf("%d",there[r]);
//printf("\n%d",file);
//fclose(file);
printf ("\n OK, Thanks! Now What Number Do You Want To Search For Frequency In Your Array? ");
scanf("\n%d", &x);/*Stores Number To Search For Frequency*/
frequency(integers,????????,x);
getch();
fclose(file);
}
?????? is the size of the integer array from where i read the file and stored it.
I could not find a way to calculate the size of the array into which i copied my file. My idea is to calculate the frequency of a number in that file and calculate the probability of it's occurrence and thereby calculating entropy..Suggestions please!
I don't know why you are initializing so many variables and some of them with awkward names like ??????.
Your main problem is that the call to function should be
frequency(integers, i, x);
Your code with the awkward irrelevant parts removed will look like
#include <stdio.h>
#include<conio.h>
void frequency (int theArray [ ], int number, int x)
{
int count = 0;
int u;
for (u = 0; u < number; u++)
{
if ( theArray[u]==x)
count++;
}
printf ("\nThe frequency of %d in your array is %d ",x,count);
}
void main()
{
FILE*file = fopen("num.txt","r");
int integers[100];
int i=0;
int num;
int x;
while(fscanf(file,"%d",&num)>0)
{
integers[i]=num;
printf("\n%d",integers[i]);
i++;
}
printf ("\n OK, Thanks! Now What Number Do You Want To Search For Frequency In Your Array? ");
scanf(" %d", &x);/*Stores Number To Search For Frequency*/
frequency(integers,i,x);
getch();
fclose(file);
}
There are a lot of parts of this code that don't make sense, but I assume it is your debugging trying to figure out what is wrong. The answer to your specific question is:
For each value read from the file you set integers[i] to the value and then increment i. Thus i is the count of items in integers. You then pass integers to frequency(), so i should be passed to the second parameter as the count.
Note that if there are more than 100 values in the file, you will over index integers and cause unpredictable behavior.
To calculate length of array:
int len= sizeof(arr)/sizeof(arr[0]);
It will give length of array without looping.