Summary: I have 2 files cs330_Lab2.h and cs330_Lab2.c The cs330_Lab2.c contains my main while my cs330_Lab2.h contains my functions. My code works fine till I go to run printHELLO() function in cs330_Lab2.h. I think it is crashing potentially after I perform the scanf() function but I am not sure. As for logic, I am about 95% sure it is right as it is a function I translated from a java project.
This is my cs330_Lab2.h file:
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
// this function takes in a int and finds the cube of every ud number leading up to it
// then it prints the string of that odd number
int cubeOfOdds(int n1){
// declaring varables
int i;
int n = n1;
for(i =1 ; i< n ; i++){
if (i%2 != 0){
int cubeI = i * i * i;
printf("Cube of %d: " , i ,": ");
printf("%d",cubeI);
printf("\n");
}
}
return 0;
}
bool isPrime(int n5) {
bool isItPrime = 1;
int i =2 ;
for (i; i <= n5 / 2; ++i) {
if (n5 % i == 0) {
isItPrime = 0;
}
}
return isItPrime;
}
bool isPowerOfTwo(int n)
{
if (n == 0)
return 0;
while (n != 1) {
if (n % 2 != 0)
return 0;
n = n / 2;
}
return 1;
}
int introToCS330 (int n2){
int n = n2;
if(n % 7 == 0){
printf("UAB");
printf("\n");
} else if (n % 3 == 0){
printf("CS");
printf("\n");
}else if (n2 % 3 == 0 && n2 % 7 == 0) {
printf("UAB CS 330");
printf("\n");
} else if (isPrime(n2) && n2 != 3 && n2 != 7) {
printf("Go Blazers");
printf("\n");
} else {
int out = n2 * n2 * n2;
printf("%d", out);
printf("\n");
}
printf("\n");
return 0;
}
int printHELLO (int n3){
int i;
for( i=0;i<=n3;i++)
{
if(isPowerOfTwo(i))
{
printf("HELLO");
}else
{
printf("%d",i);
}
}
return 0;
}
int paintGallons(float length, float width, float height){
float sqft = (((length * height * 2)+(width * height * 2)+(length*width))/400);
int neededGals = round(sqft);
return neededGals;
}
bool grader (float avg_exams, float avg_hw, int attendance) {
bool out = 0;
if (attendance >= 20) {
printf("FAIL");
if (avg_exams > 70 && avg_hw > 70 && (avg_exams > 85 || avg_hw > 85) ){
printf("PASS");
out = 1;
}
}
return out;
}
This is my cs330_Lab2.c File:
//
// Created by willb on 9/11/2022.
//
#include "cs330_Lab2.h"
int main(){
printf("\n");
printf("cubeOfOdds:");
printf("Please enter a number:");
int n1;
scanf("%d",&n1);
cubeOfOdds(n1);
printf("\n");
printf("introToJava:");
printf("Please enter a positive integer: ");
int n2;
scanf("%d",&n2);
introToCS330(n2);
printf("printHELLO:");
printf("Please enter a positive integer: ");
int n3;
scanf("%d",&n3);
printHELLO(n3);
int k = 10;
int k1 = 12;
int k2 = 8;
int h =paintGallons(k, k1, k2);
grader(72,88,22); // expected result PASS
return 0;
}
This is the instructions for printHello:
printHello (n)
Write the function printHELLO that takes an integer n (n is equal or greater than 0) and
prints
a string with the integers 0 through n, inclusive, except that every power
of 2 is replaced by
HELLO.
and the output, if int 3 is entered, should be: "0HELLOHELLO3" and the output, if int 7 is entered, should be: "0HELLOHELLO3HELLO567"
I am not asking you to solve it for me. I just need to know why it is crashing and how to fix it. I can go back and fix all logical errors once I get it to run.